using System;

using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PockeTwit.FingerUI;
using PockeTwit.FingerUI.Menu;
using PockeTwit.Library;
using PockeTwit.MediaServices;
using PockeTwit.NotificationsCode;
using PockeTwit.OtherServices;
using PockeTwit.SpecialTimelines;
using PockeTwit.TimeLines;
using Yedda;
using System.Collections;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;


namespace PockeTwit
{
    public partial class TweetList : Form
    {
        delegate void delNothing(UpgradeChecker.UpgradeInfo Info);

        private class HistoryItem
        {
            public string Argument;
            public Twitter.ActionType Action;
            public Twitter.Account Account;
            public int SelectedItemIndex = -1;
            public int itemsOffset = -1;
            public object ItemInfo = null;
        }

        private Stack<HistoryItem> History = new Stack<HistoryItem>();

        #region�Fields�(12)�
        private MsgWindow MsgWin;

        private UpgradeChecker Checker;

        private Yedda.Twitter.Account CurrentlySelectedAccount;
        private List<Yedda.Twitter> TwitterConnections = new List<Yedda.Twitter>();
        private Dictionary<Yedda.Twitter, Following> FollowingDictionary = new Dictionary<Yedda.Twitter, Following>();
        private TimelineManagement Manager;
        private NotificationHandler Notifyer;
        private bool IsLoaded;
        private string ShowUserID;
        private bool StartBackground = false;
        private ISpecialTimeLine currentSpecialTimeLine = null;
        private string urlToLaunch = string.Empty;

        #region MenuItems
        #region LeftMenu
        FingerUI.Menu.SideMenuItem BackMenuItem;

        FingerUI.Menu.SideMenuItem FriendsTimeLineMenuItem;
        FingerUI.Menu.SideMenuItem RefreshFriendsTimeLineMenuItem;
        FingerUI.Menu.SideMenuItem MessagesMenuItem;
        FingerUI.Menu.SideMenuItem RefreshMessagesMenuItem;
        
        FingerUI.Menu.SideMenuItem PublicMenuItem;
        FingerUI.Menu.SideMenuItem SearchMenuItem;
        FingerUI.Menu.SideMenuItem SendDirectMessagesMenuItem;
        FingerUI.Menu.SideMenuItem ViewFavoritesMenuItem;
        FingerUI.Menu.SideMenuItem GroupsMenuItem;
        
        FingerUI.Menu.SideMenuItem OtherGlobalMenuItem;

        FingerUI.Menu.SideMenuItem MyTimeLineMenuItem;
        FingerUI.Menu.SideMenuItem MyFollowingMenuItem;
        FingerUI.Menu.SideMenuItem MyFollowerMenuItem;
        FingerUI.Menu.SideMenuItem MyMessageMenuItem;
        FingerUI.Menu.SideMenuItem MyCommentMenuItem;
        FingerUI.Menu.SideMenuItem MyTopicMenuItem;

        FingerUI.Menu.SideMenuItem ReceivedCommentMenuItem;
        FingerUI.Menu.SideMenuItem SendCommentMenuItem;

        FingerUI.Menu.SideMenuItem MyMenuItem;

        FingerUI.Menu.SideMenuItem PostUpdateMenuItem;
        FingerUI.Menu.SideMenuItem SettingsMenuItem;
        FingerUI.Menu.SideMenuItem FollowUserMenuItem;
        FingerUI.Menu.SideMenuItem AccountsSettingsMenuItem;
        FingerUI.Menu.SideMenuItem AdvancedSettingsMenuItem;
        FingerUI.Menu.SideMenuItem AvatarSettingsMenuItem;
        FingerUI.Menu.SideMenuItem GroupSettingsMenuItem;
        FingerUI.Menu.SideMenuItem NotificationSettingsMenuItem;
        FingerUI.Menu.SideMenuItem MediaServiceSettingsMenuItem;
        FingerUI.Menu.SideMenuItem OtherSettingsMenuItem;
        FingerUI.Menu.SideMenuItem UISettingsMenuItem;
        
        FingerUI.Menu.SideMenuItem AboutMenuItem;

        FingerUI.Menu.SideMenuItem WindowMenuItem;
        FingerUI.Menu.SideMenuItem FullScreenMenuItem;
        FingerUI.Menu.SideMenuItem MinimizeMenuItem;
        FingerUI.Menu.SideMenuItem ExitMenuItem;
        #endregion
        #region RightMenu
        FingerUI.Menu.SideMenuItem ConversationMenuItem;
        FingerUI.Menu.SideMenuItem DeleteStatusMenuItem;
        FingerUI.Menu.SideMenuItem ResponsesMenuItem;

        FingerUI.Menu.SideMenuItem ReplyMenuItem;
        FingerUI.Menu.SideMenuItem ReplyAllMenuItem;
        FingerUI.Menu.SideMenuItem DirectMenuItem;

        FingerUI.Menu.SideMenuItem EmailMenuItem;
        FingerUI.Menu.SideMenuItem QuoteMenuItem;
        FingerUI.Menu.SideMenuItem RetweetMenuItem;
        FingerUI.Menu.SideMenuItem ViewCommentMenuItem;
        FingerUI.Menu.SideMenuItem ToggleFavoriteMenuItem;
        FingerUI.Menu.SideMenuItem UserTimelineMenuItem;
        FingerUI.Menu.SideMenuItem ProfilePageMenuItem;
        FingerUI.Menu.SideMenuItem FollowMenuItem;

        FingerUI.Menu.SideMenuItem SendToGroupMenuItem;
        FingerUI.Menu.SideMenuItem MoveToGroupMenuItem;
        FingerUI.Menu.SideMenuItem CopyToGroupMenuItem;
        
        private FingerUI.Menu.SideMenuItem CopyNewGroupMenuItem;
        private FingerUI.Menu.SideMenuItem MoveNewGroupMenuItem;
        #endregion
        #endregion MenuItems

        #endregion�Fields�

        #region�Constructors�(1)�
        public TweetList(bool InBackGround, string[] args)
        {
            //throw new Exception("Bam!");
            StartBackground = InBackGround;
            Microsoft.WindowsCE.Forms.MobileDevice.Hibernate += new EventHandler(MobileDevice_Hibernate);
            if (InBackGround)
            {
                this.Hide();
            }
            else
            {
                if (ClientSettings.IsMaximized)
                {
                    this.WindowState = FormWindowState.Maximized;
                    this.Menu = null;
                }
                else
                {
                    AddMainMenuItems();
                }
            }
            InitializeComponent();

            this.MsgWin = new MsgWindow(this);

            progressBar1.Visible = false;
            lblProgress.Visible = true;
            //lblProgress.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Initializing");
            lblProgress.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Initializing\nWeGo QQ Group：95223866");
            if (DetectDevice.DeviceType == DeviceType.Professional)
            {
                inputPanel1 = new Microsoft.WindowsCE.Forms.InputPanel();
                TodayScreenRegistrySetup.CheckTodayScreenInstalled();
            }
            if (UpgradeChecker.devBuild)
            {
                //this.lblTitle.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Launching PockeTwit Dev");
                this.lblTitle.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Launching WeGo");
            }
            
            SizeF currentScreen = this.CurrentAutoScaleDimensions;
            if (currentScreen.Height == 192)
            {
                statList.MaxVelocity = 45;
            }
            else
            {
                statList.MaxVelocity = 45;
            }
            ClientSettings.TextHeight = currentScreen.Height;

            
            this.Visible = !InBackGround;
            statList.Visible = false;
            PockeTwit.Themes.FormColors.SetColors(this);
            PockeTwit.Localization.XmlBasedResourceManager.LocalizeForm(this);

            this.Refresh();
            
            Application.DoEvents();
            LocalStorage.DataBaseUtility.CheckDBSchema();

            statList.Progress += new KListControl.delProgress(statList_Progress);
            SpecialTimeLinesRepository.Load();

            //First run means no settings exist
            if (ClientSettings.AccountsList.Count == 0)
            {
                ClearSettings();
                // 如果没有账号就退出
                if (ClientSettings.AccountsList.Count == 0)
                    return;
            }

            if (StartBackground) { this.Hide(); }
            CreateLeftMenu();
            CreateRightMenu();
            // TODO 初始化，需要重点跟踪
            if (!SetEverythingUp())
            {
                if (Notifyer != null) { Notifyer.ShutDown(); }
                Application.Exit();
                ThrottledArtGrabber.running = false;
                return;
            }
            SwitchToDone();

            ProcessArgs(args);

            if(!string.IsNullOrEmpty(ClientSettings.PreviousMediaService))
            {
                var switcher = new SwitchToTweetPhoto();
                switcher.Owner = this;
                switcher.ShowDialog();
                switcher.Dispose();
            }
        }

        void statList_Progress(int itemnumber, int totalnumber)
        {
            if (itemnumber >= totalnumber)
            {
                lblTitle.Visible = false;
                progressBar1.Visible = false;
                return;
            }
            lblTitle.Visible = true;
            progressBar1.Visible = true;
            progressBar1.Maximum = totalnumber;
            progressBar1.Minimum = 0;
            progressBar1.Value = itemnumber;
            if (lblProgress.Visible)
            {
                lblProgress.Visible = false;
                this.Refresh();
            }
            
        }

        private void AddMainMenuItems()
        {
            if(this.Menu==null)
            {
                this.Menu = mainMenu1;
            }
        }

        void MobileDevice_Hibernate(object sender, EventArgs e)
        {
            //MessageBox.Show("The device is running low on memory. You may want to close PockeTwit or other applications.");
        }

        #endregion�Constructors�

        #region�Delegates�and�Events�(2)�


        //�Delegates�(2)�
        private delegate void delSetWindowState(FormWindowState state);
        private delegate void delAddStatuses(Library.status[] arrayOfStats, bool KeepPosition);
        private delegate void delChangeCursor(Cursor CursorToset);
        private delegate void delNotify(int Count);
        private delegate bool delBool();
        private delegate void delNone();

        #endregion�Delegates�and�Events�

        #region�Methods�(38)�


        //�Private�Methods�(38)�


        public bool IsFocused()
        {
            if (InvokeRequired)
            {
                delBool d = new delBool(IsFocused);
                bool invokeRetured = (bool)this.Invoke(d, null);
                return invokeRetured;
            }
            else
            {
                try
                {
                    return this.Focused | statList.Focused;
                }
                catch (ObjectDisposedException)
                {
                    return false;
                }
            }

        }

        private Yedda.Twitter GetMatchingConnection(Yedda.Twitter.Account AccountInfo)
        {
            if (AccountInfo == null)
            {
                return TwitterConnections[0];
            }
            foreach (Yedda.Twitter conn in TwitterConnections)
            {
                if (conn.AccountInfo.Equals(AccountInfo))
                {
                    return conn;
                }
            }
            return TwitterConnections[0];
        }

        private void AddStatusesToList(Library.status[] mergedstatuses)
        {
            AddStatusesToList(mergedstatuses, false);
        }

        // TODO 【case】刷新右菜单，没有选中微博时，右菜单Disable。
        private void AddStatusesToList(Library.status[] mergedstatuses, bool KeepPosition)
        {
            if (mergedstatuses == null) //Why would this turn up null? Comm error?
            {
                //MessageBox.Show("Status list was null for " + statList.CurrentList());
                return;
            } 
            if (mergedstatuses.Length == 0) { return; }
            if (InvokeRequired)
            {
                delAddStatuses d = AddStatusesToList;
                this.Invoke(d, new object[] { mergedstatuses, KeepPosition });
            }
            else
            {
                int newItems = 0;
                if(KeepPosition)
                {
                    IDisplayItem selectedItem = statList.SelectedItem;
                    if (selectedItem != null && selectedItem is StatusItem)
                    {
                        TimelineManagement.TimeLineType t = TimelineManagement.TimeLineType.Friends;
                        if (statList.CurrentList() == "Messages_TimeLine")
                        {
                            t = TimelineManagement.TimeLineType.Messages;
                        }
                        string constraints = "";
                        if (currentSpecialTimeLine != null)
                        {
                            constraints = currentSpecialTimeLine.GetConstraints();
                            t = (currentSpecialTimeLine.Timelinetype == SpecialTimelines.SpecialTimeLinesRepository.TimeLineType.UserGroup) ? TimelineManagement.TimeLineType.Friends : TimelineManagement.TimeLineType.Searches;
                        }
                        newItems = LocalStorage.DataBaseUtility.CountItemsNewerThan(t, (selectedItem as StatusItem).Tweet.id, constraints);
                    }
                }
            
                int oldOffset = 0;
                int oldIndex = 0;
                
                oldIndex = statList.SelectedIndex;
                oldOffset = statList.YOffset - (ClientSettings.ItemHeight * oldIndex);

                statList.Clear();
                foreach (Library.status stat in mergedstatuses)
                {
                    if (stat == null || stat.user == null || stat.user.screen_name == null) continue;
                    StatusItem item = new StatusItem {Tweet = stat};
                    statList.AddItem(item);
                }
                IDisplayItem currentItem = null;
                if (!ClientSettings.AutoScrollToTop)
                {
                    if (oldIndex >= 0 && KeepPosition && newItems < ClientSettings.MaxTweets)
                    {
                        try
                        {
                            statList.SelectedItem = statList[newItems];
                            currentItem = statList.SelectedItem;
                            statList.YOffset = oldOffset + (newItems * ClientSettings.ItemHeight);
                        }
                        catch (KeyNotFoundException)
                        {
                            //What to do?
                        }
                    }
                    else
                    {
                        statList.JumpToLastSelected();
                        currentItem = statList[0];
                    }
                }
                else
                {
                    statList.SelectedItem = statList[0];
                    currentItem = statList.SelectedItem;
                    statList.YOffset = 0;
                }

                if (currentItem != null)
                {
                    if (currentItem is StatusItem)
                        CurrentlySelectedAccount = (currentItem as StatusItem).Tweet.Account;
                    UpdateRightMenu();
                }
                statList.Redraw();
                statList.RerenderPortal();
                statList.Repaint();
            }
        }

        private void ChangeCursor(Cursor CursorToset)
        {
            if (InvokeRequired)
            {
                delChangeCursor d = new delChangeCursor(ChangeCursor);
                this.Invoke(d, new object[] { CursorToset });
            }
            else
            {
                Cursor.Current = CursorToset;
            }
        }

        private void ChangeSettings(BaseSettingsForm f)
        {
            this.statList.Visible = false;
            IsLoaded = false;
            f.Owner = this;
            if(f.ShowDialog()==DialogResult.Cancel)
            {
                this.statList.Visible = true;
                IsLoaded = true;
                f.Dispose();
                return;
            }
            if(f.NeedsReRender)
            {
                statList.BackColor = ClientSettings.BackColor;
                statList.ForeColor = ClientSettings.ForeColor;
                CreateRightMenu();
                CreateLeftMenu();
                PockeTwit.Themes.FormColors.SetColors(this);
                PockeTwit.Localization.XmlBasedResourceManager.LocalizeForm(this);

                ReloadTimeLine();
                statList.ResetFullScreenColors();
                statList.RerenderBySize();
            }
            if(f.NeedsReset)
            {
                PockeTwit.Localization.LocalizedMessageBox.Show("Your settings changes require that you restart the application.");
                ExitApplication();
            }
            this.statList.Visible = true;
            statList.Redraw();
            f.Dispose();
        }

        private void ToggleFavorite()
        {
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return; }
            if (selectedItem.IsFavorite)
            {
                DestroyFavorite();
                ToggleFavoriteMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Make Favorite");
            }
            else
            {
                CreateFavoriteAsync();
                ToggleFavoriteMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Remove Favorite");
            }
        }

        private void CreateFavorite(string ID, Yedda.Twitter.Account AccountInfo)
        {
            GetMatchingConnection(AccountInfo).SetFavorite(ID);
            UpdateRightMenu();
            statList.Repaint();
            ChangeCursor(Cursors.Default);
        }



        private void CreateFavoriteAsync()
        {
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return; }
            Yedda.Twitter.Account ChosenAccount = selectedItem.Tweet.Account;

            ChangeCursor(Cursors.WaitCursor);
            selectedItem.IsFavorite = true;

            string ID = selectedItem.Tweet.id;
            System.Threading.ThreadStart ts = delegate { CreateFavorite(ID, ChosenAccount); };
            System.Threading.Thread t = new System.Threading.Thread(ts);
            t.Name = "CreateFavorite";
            t.Start();
        }

        private void DestroyFavorite()
        {
            ChangeCursor(Cursors.WaitCursor);
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            string ID = selectedItem.Tweet.id;
            GetMatchingConnection(selectedItem.Tweet.Account).DestroyFavorite(ID);
            selectedItem.IsFavorite = false;
            UpdateRightMenu();
            statList.Repaint();
            ChangeCursor(Cursors.Default);
        }

        private void CreateNewGroup(bool Exclusive)
        {
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return; }
            if (selectedItem.Tweet.user == null) { return; }

            using (DefineGroup d = new DefineGroup())
            {
                d.Owner = this;
                if (d.ShowDialog() == DialogResult.OK)
                {
                    UserGroupTimeLine t = new UserGroupTimeLine();
                    t.name = d.GroupName;

                    if(AddUserToGroup(t, Exclusive, false))
                    {
                        SpecialTimeLinesRepository.Add(t);

                        AddGroupSelectMenuItem(t);

                        AddAddUserToGroupMenuItem(t);
                        SpecialTimeLinesRepository.Save();
                        if(Exclusive)
                        {
                            Cursor.Current = Cursors.WaitCursor;
                            ReloadTimeLine();
                            Cursor.Current = Cursors.Default;
                        }
                    }
                }
                d.Dispose();
            }
        }
       
        internal void ShowSpecialTimeLine(ISpecialTimeLine t, Yedda.Twitter.PagingMode pagingMode)
        {
            UpdateHistoryPosition();
            currentSpecialTimeLine = t;
            ChangeCursor(Cursors.WaitCursor);
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Search;
            i.Argument = t.name;
            i.ItemInfo = t;
            
            History.Push(i);

            SwitchToList(t.ListName);
            statList.ClearVisible();
            AddStatusesToList(Manager.GetGroupedTimeLine(t, pagingMode), false);
            if (t.Timelinetype == SpecialTimeLinesRepository.TimeLineType.SavedSearch)
                statList.AddItem(new MoreResultsItem(this,t));
            ChangeCursor(Cursors.Default);
        }

        private bool AddUserToGroup(UserGroupTimeLine t, bool Exclusive)
        {
            return AddUserToGroup(t, Exclusive, true);
        }
        private bool AddUserToGroup(UserGroupTimeLine t, bool Exclusive, bool ReloadImmediately)
        {
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return false; }
            if (selectedItem.Tweet.user == null) { return false; }

            string Message = "";
            switch (Exclusive)
            {
                case true:
                    Message="This will move {0} out of the Friends timeline and into the {1} group.\n\nAre you sure you want to proceed?";
                    break;
                case false:
                    Message = "This will copy {0} into the {1} group and still show them in the Friends timeline.\n\nAre you sure you want to proceed?";

                    break;

            }
            if (PockeTwit.Localization.LocalizedMessageBox.Show(Message, "Group User", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, selectedItem.Tweet.user.screen_name, t.name) == DialogResult.Yes)
            {
                t.AddItem(selectedItem.Tweet.user.id, selectedItem.Tweet.user.screen_name, Exclusive);
                SpecialTimeLinesRepository.Save();
            }
            else
            {
                return false;
            }
            if(Exclusive && ReloadImmediately)
            {
                Cursor.Current = Cursors.WaitCursor;
                ReloadTimeLine();
                Cursor.Current = Cursors.Default;
            }
            return true;
        }

        private void ReloadTimeLine()
        {
            if(statList.CurrentList()=="Friends_TimeLine")
            {
                AddStatusesToList(Manager.GetFriendsImmediately(), true);
            }
            else
            {
                if (statList.CurrentList().StartsWith("Grouped") || statList.CurrentList().StartsWith("SavedSearch_TimeLine_"))
                {
                    AddStatusesToList(Manager.GetGroupedTimeLine(currentSpecialTimeLine, Yedda.Twitter.PagingMode.None), true);
                    if (currentSpecialTimeLine.Timelinetype == SpecialTimeLinesRepository.TimeLineType.SavedSearch)
                        statList.AddItem(new MoreResultsItem(this, currentSpecialTimeLine));

                }
            }
            
        }
        private delegate void delFollowUser(Yedda.Twitter.Account a);
        
        private void SetFollowMenu()
        {
            if(ClientSettings.AccountsList.Count==1)
            {
                return;
            }
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return; }
            if (selectedItem.Tweet.user == null) { return; }
            Yedda.Twitter Conn = GetMatchingConnection(selectedItem.Tweet.Account);
            
            FollowMenuItem.SubMenuItems.Clear();
            List<Yedda.Twitter.Account> MatchingAccounts = new List<Twitter.Account>();

            foreach (var account in ClientSettings.AccountsList)
            {
                if(account.Server==Conn.AccountInfo.Server)
                {
                    MatchingAccounts.Add(account);
                }
            }

            if (MatchingAccounts.Count > 1)
            {
                FollowMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Follow...");
                FollowMenuItem.ClickedMethod = null;
                foreach (var account in MatchingAccounts)
                {
                    var account1 = account;
                    delMenuClicked d = () => FollowUser(account1);
                    var item = new SideMenuItem(d, account.ToString(), statList.RightMenu);
                    FollowMenuItem.SubMenuItems.Add(item);
                }
                return;
            }

            delMenuClicked  followClicked = () => FollowUser(MatchingAccounts[0]);
            FollowMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Follow");
            FollowMenuItem.ClickedMethod = followClicked;

        }

        private void ToggleFollow()
        {
            StatusItem selectedItem = (StatusItem)statList.SelectedItem;
            if (selectedItem == null) { return; }
            if (selectedItem.Tweet.user == null) { return; }
            Yedda.Twitter Conn = GetMatchingConnection(selectedItem.Tweet.Account);
            if (FollowingDictionary[Conn].IsFollowing(selectedItem.Tweet.user))
            {
                StopFollowingUser();
                FollowMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Follow");
            }
            else
            {
                FollowUser();
                FollowMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Stop Following");
            }
        }
        private void FollowUser()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            if (selectedItem.Tweet.user == null) { return; }
            Yedda.Twitter Conn = GetMatchingConnection(selectedItem.Tweet.Account);
            FollowUser(Conn.AccountInfo);
        }
        private void FollowUser(Yedda.Twitter.Account account)
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            if (selectedItem.Tweet.user == null) { return; }
            ChangeCursor(Cursors.WaitCursor);
            Yedda.Twitter Conn = GetMatchingConnection(selectedItem.Tweet.Account);
            //if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sohu)
            //    Conn.FollowUser(selectedItem.Tweet.user.id);
            //else
            //    Conn.FollowUser(selectedItem.Tweet.user.screen_name);
            Conn.FollowUser(selectedItem.Tweet.user.id);

            FollowingDictionary[Conn].AddUser(selectedItem.Tweet.user);
            UpdateRightMenu();
            GlobalEventHandler.CallShowErrorMessage("Following succeed.");
            ChangeCursor(Cursors.Default);
        }
        private void StopFollowingUser()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            Yedda.Twitter Conn = GetMatchingConnection(selectedItem.Tweet.Account);
            if (PockeTwit.Localization.LocalizedMessageBox.Show("Are you sure you want to stop following {0}?", "Stop Following", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, selectedItem.Tweet.user.screen_name) == DialogResult.Yes)
            {
                ChangeCursor(Cursors.WaitCursor);
                //if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sohu)
                //    Conn.StopFollowingUser(selectedItem.Tweet.user.id);
                //else
                //    Conn.StopFollowingUser(selectedItem.Tweet.user.screen_name);
                Conn.StopFollowingUser(selectedItem.Tweet.user.id);

                FollowingDictionary[Conn].StopFollowing(selectedItem.Tweet.user);
                UpdateRightMenu();
                ChangeCursor(Cursors.Default);
            }

        }


        private void SendDirectMessage()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            
            string User = selectedItem.Tweet.user.screen_name;
            SetStatus("d " + User, selectedItem.Tweet.id);
        }

        private void SendReplyAll()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            string User = selectedItem.Tweet.user.screen_name;
            string currentUser = "@" + selectedItem.Tweet.Account.UserName;
            if (selectedItem.Tweet.isDirect)
            {
                if (PockeTwit.Localization.LocalizedMessageBox.Show("Are you sure you want to reply to a Direct Message?", "Reply?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    SendDirectMessage();
                    return;
                }
            }
            
            //if the tweet contains a username, check for all users mentioned and reply to all 
            if (selectedItem.Tweet.text.IndexOf("@") >= 0)
            {
                char[] IgnoredAtChars = new[] { ':', ',', '-', '.', '!', '?', '~', '=', '&', '*', '>', ')', '(' };
                System.Text.RegularExpressions.Regex GetClickables =
                new System.Text.RegularExpressions.Regex(@"(@\w+)", System.Text.RegularExpressions.RegexOptions.Compiled | System.Text.RegularExpressions.RegexOptions.IgnoreCase);

                System.Text.RegularExpressions.MatchCollection m = GetClickables.Matches(selectedItem.Tweet.text);

                List<string> usersToReplyTo = new List<string>();
                usersToReplyTo.Add("@" + User);
                int found = 0;

                foreach (System.Text.RegularExpressions.Match match in m)
                {
                    string u = match.Value.Trim(IgnoredAtChars);
                    //Can't use Contains because it the comparison is case sensitive
                    //Found this method at http://www.developersdex.com/csharp/message.asp?p=1111&r=6512606
                    //if (!usersToReplyTo.Contains(match.Value.Trim(IgnoredAtChars)))
                    found = usersToReplyTo.FindIndex(delegate(string s)
                    {
                        return 0 ==
                            string.Compare(u, s, StringComparison.OrdinalIgnoreCase);
                    });
                    if (found < 0 && String.Compare(u, currentUser, true) != 0)
                    {
                        usersToReplyTo.Add(u);
                    }
                }
                string userList = "";
                usersToReplyTo.ForEach(delegate(String s) { userList += s + " "; });
                userList = userList.Trim();
                SetStatus(userList, selectedItem.Tweet.id);
            }
            else
            {
                SetStatus("@" + User, selectedItem.Tweet.id);
            }
        }

        private void SendReply()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }
            string User = selectedItem.Tweet.user.screen_name;
            if (selectedItem.Tweet.isDirect)
            {
                if (PockeTwit.Localization.LocalizedMessageBox.Show("Are you sure you want to reply to a Direct Message?", "Reply?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) == DialogResult.No)
                {
                    SendDirectMessage();
                    return;
                }
            }

            SetStatus("@" + User, selectedItem.Tweet.id);
        }

        // TODO: 左菜单
        private void CreateLeftMenu()
        {
            //BACK
            BackMenuItem = new FingerUI.Menu.SideMenuItem(this.GoBackInHistory, "Back", statList.LeftMenu);
            BackMenuItem.CanHide = true;


            //Friends
            FriendsTimeLineMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowFriendsTimeLine, "Friends Timeline", statList.LeftMenu, "Friends_TimeLine");
            RefreshFriendsTimeLineMenuItem = new FingerUI.Menu.SideMenuItem(this.RefreshFriendsTimeLine, "Refresh Friends", statList.LeftMenu, "Friends_TimeLine");
            //Messages
            MessagesMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMessagesTimeLine, "Messages", statList.LeftMenu, "Messages_TimeLine");
            RefreshMessagesMenuItem = new FingerUI.Menu.SideMenuItem(this.RefreshMessagesTimeLine, "Refresh Messages", statList.LeftMenu, "Messages_TimeLine");
            //Groups
            GroupsMenuItem = new FingerUI.Menu.SideMenuItem(null, "Groups ...", statList.LeftMenu);
            GroupsMenuItem.Visible = false;
            //TimeLinesMenuItem.SubMenuItems.Add(GroupsMenuItem);

            //More
            SearchMenuItem = new FingerUI.Menu.SideMenuItem(this.TwitterSearch, "Search/Local", statList.LeftMenu);
            PublicMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowPublicTimeLine, "Public Timeline", statList.LeftMenu);
            SendDirectMessagesMenuItem = new SideMenuItem(this.ShowSendDirectMessagesTimeLine, "Sent Direct Messages", statList.LeftMenu);
            //ViewFavoritesMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowTrends, "View Favorites", statList.LeftMenu);
            FollowUserMenuItem = new SideMenuItem(this.FollowUserClicked, "Follow User", statList.LeftMenu);
            AboutMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowAbout, "About/Feedback", statList.LeftMenu);

            OtherGlobalMenuItem = new FingerUI.Menu.SideMenuItem(null, "Other ...", statList.LeftMenu);
            //OtherGlobalMenuItem.SubMenuItems.Add(SearchMenuItem);
            OtherGlobalMenuItem.SubMenuItems.Add(PublicMenuItem);
            //OtherGlobalMenuItem.SubMenuItems.Add(SendDirectMessagesMenuItem);
            //OtherGlobalMenuItem.SubMenuItems.Add(ViewFavoritesMenuItem);
            OtherGlobalMenuItem.SubMenuItems.Add(FollowUserMenuItem);
            OtherGlobalMenuItem.SubMenuItems.Add(AboutMenuItem);

            // TODO 需要实现"我的 ..."菜单
            MyTimeLineMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMyTimeLine, "My Timeline", statList.LeftMenu);
            MyFollowingMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMyFriends, "My Following", statList.LeftMenu);
            MyFollowerMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMyFollowers, "My Follower", statList.LeftMenu);
            MyMessageMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMessagesTimeLine, "My Messages", statList.LeftMenu);
            MyTopicMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowPublicTimeLine, "My Topic", statList.LeftMenu);
            //ViewFavoritesMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowFavorites, "View Favorites", statList.LeftMenu);
            ViewFavoritesMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowFavorites, "My Favorites", statList.LeftMenu);

            //MyCommentMenuItem = new FingerUI.Menu.SideMenuItem(ShowMyComments, "My Comment", statList.LeftMenu);
            MyCommentMenuItem = new FingerUI.Menu.SideMenuItem(null, "My Comment", statList.LeftMenu);
            ReceivedCommentMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMyReceivedComments, "Received Comment", statList.LeftMenu);
            SendCommentMenuItem = new FingerUI.Menu.SideMenuItem(this.ShowMySendComments, "Send Comment", statList.LeftMenu);
            MyCommentMenuItem.SubMenuItems.Add(ReceivedCommentMenuItem);
            MyCommentMenuItem.SubMenuItems.Add(SendCommentMenuItem);

            MyMenuItem = new FingerUI.Menu.SideMenuItem(null, "My ...", statList.LeftMenu);
            MyMenuItem.SubMenuItems.Add(MyTimeLineMenuItem);
            MyMenuItem.SubMenuItems.Add(MyFollowingMenuItem);
            MyMenuItem.SubMenuItems.Add(MyFollowerMenuItem);
            //MyMenuItem.SubMenuItems.Add(MyMessageMenuItem);
            MyMenuItem.SubMenuItems.Add(MyCommentMenuItem);
            MyMenuItem.SubMenuItems.Add(ViewFavoritesMenuItem);
            //MyMenuItem.SubMenuItems.Add(MyTopicMenuItem);

            PostUpdateMenuItem = new FingerUI.Menu.SideMenuItem(this.SetStatus, "Post Update", statList.LeftMenu);
            
            //MapMenuItem = new FingerUI.Menu.SideMenuItem(this.MapList, "Map These", statList.LeftMenu);

            delMenuClicked showAccounts = () => this.ChangeSettings(new AccountsForm());
            delMenuClicked showAdvanced = () => this.ChangeSettings(new SettingsHandler.AdvancedForm());
            delMenuClicked showAvatar = () => this.ChangeSettings(new AvatarSettings());
            delMenuClicked showNotification = () => this.ChangeSettings(new SettingsHandler.NotificationSettings());
            delMenuClicked showOther = () => this.ChangeSettings(new OtherSettings());
            delMenuClicked showUISettings = () => this.ChangeSettings(new UISettings());
            delMenuClicked showGroupSettings = () => this.ChangeSettings(new SettingsHandler.GroupManagement());
            delMenuClicked showMediaServiceSettings = () => this.ChangeSettings(new MediaService());

            WindowMenuItem = new FingerUI.Menu.SideMenuItem(null, "Window ...", statList.LeftMenu);

            FullScreenMenuItem = new FingerUI.Menu.SideMenuItem(ToggleFullScreen, "Toggle FullScreen", statList.LeftMenu);
            MinimizeMenuItem = new FingerUI.Menu.SideMenuItem(this.Minimize, "Minimize", statList.LeftMenu);
            ExitMenuItem = new FingerUI.Menu.SideMenuItem(this.ExitApplication, "Exit", statList.LeftMenu);

            WindowMenuItem.SubMenuItems.Add(FullScreenMenuItem);
            WindowMenuItem.SubMenuItems.Add(MinimizeMenuItem);
            
            //SettingsMenuItem = new FingerUI.Menu.SideMenuItem(this.ChangeSettings, "Settings", statList.LeftMenu);
            SettingsMenuItem = new FingerUI.Menu.SideMenuItem(null, "Settings...", statList.LeftMenu);
            AccountsSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showAccounts, "Accounts", statList.LeftMenu);
            AdvancedSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showAdvanced, "Advanced", statList.LeftMenu);
            AvatarSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showAvatar, "Avatar", statList.LeftMenu);
            GroupSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showGroupSettings, "Manage Groups", statList.LeftMenu);
            MediaServiceSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showMediaServiceSettings, "Media Service", statList.LeftMenu);
            NotificationSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showNotification, "Notifications", statList.LeftMenu);
            OtherSettingsMenuItem = new FingerUI.Menu.SideMenuItem(showOther, "Other", statList.LeftMenu);
            UISettingsMenuItem = new FingerUI.Menu.SideMenuItem(showUISettings, "UI", statList.LeftMenu);
            SettingsMenuItem.SubMenuItems.Add(AccountsSettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(AvatarSettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(GroupSettingsMenuItem);
            //SettingsMenuItem.SubMenuItems.Add(MediaServiceSettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(NotificationSettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(UISettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(OtherSettingsMenuItem);
            SettingsMenuItem.SubMenuItems.Add(AdvancedSettingsMenuItem);
            
            

            foreach (ISpecialTimeLine t in SpecialTimeLinesRepository.GetList())
            {
                AddGroupSelectMenuItem(t);
            }


            //statList.LeftMenu.ResetMenu(new FingerUI.Menu.SideMenuItem[]{BackMenuItem, FriendsTimeLineMenuItem, 
            //    RefreshFriendsTimeLineMenuItem, MessagesMenuItem, RefreshMessagesMenuItem, GroupsMenuItem, 
            //    OtherGlobalMenuItem, PostUpdateMenuItem, SettingsMenuItem,
            //    WindowMenuItem, ExitMenuItem});
            statList.LeftMenu.ResetMenu(new FingerUI.Menu.SideMenuItem[]{BackMenuItem, FriendsTimeLineMenuItem, 
                RefreshFriendsTimeLineMenuItem, MessagesMenuItem, RefreshMessagesMenuItem, MyMenuItem, GroupsMenuItem, 
                OtherGlobalMenuItem, PostUpdateMenuItem, SettingsMenuItem,
                WindowMenuItem, ExitMenuItem});
        }

        private void AddGroupSelectMenuItem(ISpecialTimeLine t)
        {
            delMenuClicked showItemClicked = delegate()
            {
                ShowSpecialTimeLine(t, Yedda.Twitter.PagingMode.None);
            };

            GroupsMenuItem.Visible = true;
            FingerUI.Menu.SideMenuItem item = new FingerUI.Menu.SideMenuItem(showItemClicked, t.name, statList.LeftMenu, t.ListName);
            GroupsMenuItem.SubMenuItems.Add(item);
        }

        // TODO: 右菜单
        private void CreateRightMenu()
        {
            /*
            if ((statList != null) && (statList.SelectedItem != null) && statList.SelectedItem.GetType() != typeof(StatusItem))
            {
                statList.RightMenu.ResetMenu(null);
                statList.SelectedItem.CreateRightMenu(statList.RightMenu);
                if (statList.RightMenu.Count == 0)
                    this.specificMenu.Enabled = false;
                else
                    this.specificMenu.Enabled = true;
                return;
            }
            */
            // "Show Conversation", "Reply @User", "Direct @User", "Quote", 
            //   "Make Favorite", "@User TimeLine", "Profile Page", "Stop Following",
            // "Minimize" 
            this.specificMenu.Enabled = true;
            ConversationMenuItem = new FingerUI.Menu.SideMenuItem(GetConversation, "Show Conversation", statList.RightMenu);
            ConversationMenuItem.CanHide = true;

            DeleteStatusMenuItem = new FingerUI.Menu.SideMenuItem(DeleteStatus, "Delete Tweet", statList.RightMenu);
            DeleteStatusMenuItem.CanHide = true;

            ResponsesMenuItem = new FingerUI.Menu.SideMenuItem(null, "Respond to @User...", statList.RightMenu);
            ResponsesMenuItem.CanHide = true;

            ReplyMenuItem = new FingerUI.Menu.SideMenuItem(SendReply, "Reply @User", statList.RightMenu);
            ReplyAllMenuItem = new FingerUI.Menu.SideMenuItem(SendReplyAll, "Reply All", statList.RightMenu);
            DirectMenuItem = new FingerUI.Menu.SideMenuItem(SendDirectMessage, "Direct @User", statList.RightMenu);

            ResponsesMenuItem.SubMenuItems.Add(ReplyMenuItem);
            ResponsesMenuItem.SubMenuItems.Add(ReplyAllMenuItem);
            //ResponsesMenuItem.SubMenuItems.Add(DirectMenuItem);

            EmailMenuItem = new FingerUI.Menu.SideMenuItem(EmailThisItem, "Email Status", statList.RightMenu);
            //QuoteMenuItem = new FingerUI.Menu.SideMenuItem(Quote, "Quote", statList.RightMenu);
            QuoteMenuItem = new FingerUI.Menu.SideMenuItem(Quote, "Comment", statList.RightMenu);
            //RetweetMenuItem = new FingerUI.Menu.SideMenuItem(Retweet, "Retweet", statList.RightMenu);
            RetweetMenuItem = new FingerUI.Menu.SideMenuItem(Retweet, "Repost", statList.RightMenu);
            ViewCommentMenuItem = new FingerUI.Menu.SideMenuItem(ViewComments, "View Comments", statList.RightMenu);
            ToggleFavoriteMenuItem = new FingerUI.Menu.SideMenuItem(ToggleFavorite, "Make Favorite", statList.RightMenu);
            UserTimelineMenuItem = new FingerUI.Menu.SideMenuItem(ShowUserTimeLine, "@User Timeline", statList.RightMenu);
            ProfilePageMenuItem = new FingerUI.Menu.SideMenuItem(ShowProfile, "@User Profile", statList.RightMenu);
            FollowMenuItem = new FingerUI.Menu.SideMenuItem(ToggleFollow, "Follow @User", statList.RightMenu);

            SendToGroupMenuItem = new FingerUI.Menu.SideMenuItem(null, "Send to Group...", statList.RightMenu);
            MoveToGroupMenuItem = new FingerUI.Menu.SideMenuItem(null, "Move to Group...", statList.RightMenu);
            CopyToGroupMenuItem = new FingerUI.Menu.SideMenuItem(null, "Copy to Group...", statList.RightMenu);

            SendToGroupMenuItem.SubMenuItems.Add(MoveToGroupMenuItem);
            SendToGroupMenuItem.SubMenuItems.Add(CopyToGroupMenuItem);

            delMenuClicked copyItemClicked = () => CreateNewGroup(false);

            delMenuClicked moveItemClicked = () => CreateNewGroup(true);

            CopyNewGroupMenuItem = new FingerUI.Menu.SideMenuItem(copyItemClicked, "New Group", statList.RightMenu);
            MoveNewGroupMenuItem = new FingerUI.Menu.SideMenuItem(moveItemClicked, "New Group", statList.RightMenu);
            MoveToGroupMenuItem.SubMenuItems.Add(MoveNewGroupMenuItem);
            CopyToGroupMenuItem.SubMenuItems.Add(CopyNewGroupMenuItem);
            foreach (UserGroupTimeLine t in SpecialTimeLinesRepository.GetList(SpecialTimeLinesRepository.TimeLineType.UserGroup))
            {
                AddAddUserToGroupMenuItem(t);
            }

            // TODO 需要实现转发微博功能
            //RetweetMenuItem.Visible = false;

            statList.RightMenu.ResetMenu(new FingerUI.Menu.SideMenuItem[]{ConversationMenuItem, DeleteStatusMenuItem, ResponsesMenuItem, QuoteMenuItem, RetweetMenuItem, ViewCommentMenuItem, EmailMenuItem, ToggleFavoriteMenuItem, 
                UserTimelineMenuItem, ProfilePageMenuItem, FollowMenuItem, SendToGroupMenuItem});
        }

        private void AddAddUserToGroupMenuItem(UserGroupTimeLine t)
        {
            delMenuClicked copyItemClicked = () => AddUserToGroup(t, false);

            delMenuClicked moveItemClicked = () => AddUserToGroup(t, true);

            FingerUI.Menu.SideMenuItem copyitem = new FingerUI.Menu.SideMenuItem(copyItemClicked, t.name, statList.RightMenu);
            FingerUI.Menu.SideMenuItem moveitem = new FingerUI.Menu.SideMenuItem(moveItemClicked, t.name, statList.RightMenu);
            MoveToGroupMenuItem.SubMenuItems.Add(moveitem);
            CopyToGroupMenuItem.SubMenuItems.Add(copyitem);

        }


        private void SetLeftMenu()
        {
            BackMenuItem.Visible = History.Count > 1;
            
            FriendsTimeLineMenuItem.Visible = statList.CurrentList() != "Friends_TimeLine";
            RefreshFriendsTimeLineMenuItem.Visible = statList.CurrentList() == "Friends_TimeLine";

            MessagesMenuItem.Visible = statList.CurrentList() != "Messages_TimeLine";
            RefreshMessagesMenuItem.Visible = statList.CurrentList() == "Messages_TimeLine";
        }

        private void UpdateRightMenu()
        {
            IDisplayItem selectedItem = statList.SelectedItem;
            if (selectedItem == null) { return; }

            StatusItem item = selectedItem as StatusItem;
            if (item != null)
            {
                Yedda.Twitter conn = GetMatchingConnection(item.Tweet.Account);
                statList.SetRightMenuUser();
                if (string.IsNullOrEmpty(item.Tweet.in_reply_to_status_id))
                {
                    ConversationMenuItem.Visible = false;
                }
                else
                {
                    ConversationMenuItem.Visible = true;
                }

                //if (ClientSettings.GetAcountForUser(item.Tweet.user.screen_name) != null)
                if (ClientSettings.GetAcountFromIDForUser(item.Tweet.user.id) != null)
                {
                    DeleteStatusMenuItem.Visible = true;
                    ResponsesMenuItem.Visible = false;
                    FollowMenuItem.Visible = false;
                    SendToGroupMenuItem.Visible = false;
                }
                else
                {
                    DeleteStatusMenuItem.Visible = false;
                    ResponsesMenuItem.Visible = true;
                    FollowMenuItem.Visible = true;
                    SendToGroupMenuItem.Visible = true;
                }

                if (item.Tweet.TypeofMessage == StatusTypes.Comment)
                {
                    DeleteStatusMenuItem.Visible = false;
                    QuoteMenuItem.Visible = false;
                    RetweetMenuItem.Visible = false;
                    ViewCommentMenuItem.Visible = false;
                    ToggleFavoriteMenuItem.Visible = false;
                }
                else
                {
                    if (item.Tweet.Account.Server == Twitter.TwitterServer.digu)
                    {
                        QuoteMenuItem.Visible = false;
                        RetweetMenuItem.Visible = false;
                        ViewCommentMenuItem.Visible = false;
                    }
                    else
                    {
                        QuoteMenuItem.Visible = true;
                        RetweetMenuItem.Visible = true;
                        ViewCommentMenuItem.Visible = true;
                    }
                    ToggleFavoriteMenuItem.Visible = true;
                }


                if (conn.FavoritesWork)
                {
                    if (item.IsFavorite)
                    {
                        ToggleFavoriteMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Remove Favorite");
                    }
                    else
                    {
                        ToggleFavoriteMenuItem.Text = PockeTwit.Localization.XmlBasedResourceManager.GetString("Make Favorite");
                    }
                }




                //FollowMenuItem.Visible = true;
                if (FollowingDictionary[conn].IsFollowing(item.Tweet.user))
                {
                    FollowMenuItem.Text = "Stop Following";
                    delMenuClicked followClicked = StopFollowingUser;
                    FollowMenuItem.SubMenuItems.Clear();

                    MoveToGroupMenuItem.Visible = true;
                    CopyToGroupMenuItem.Visible = true;
                }
                else
                {
                    FollowMenuItem.Text = "Follow";
                    SetFollowMenu();

                    MoveToGroupMenuItem.Visible = false;
                    CopyToGroupMenuItem.Visible = false;
                }
            }
        }

        private void SetConnectedMenus()
        {
            if (TwitterConnections.Count > 0)
            {
                SetConnectedMenus(TwitterConnections[0], null);
            }
        }

        private void SetConnectedMenus(Yedda.Twitter t, StatusItem item)
        {
            SetLeftMenu();
            UpdateRightMenu();
        }

        private bool SetEverythingUp()
        {
            HistoryItem firstItem = new HistoryItem();
            firstItem.Action = Yedda.Twitter.ActionType.Friends_Timeline;
            History.Push(firstItem);
            if (System.IO.File.Exists(ClientSettings.AppPath + "\\crash.txt"))
            {
                using (CrashReport errorForm = new CrashReport())
                {
                    //errorForm.Owner = this;
                    errorForm.ShowDialog();
                    errorForm.Dispose();
                }
            }
            if (!StartBackground)
            {
                this.Show();
            }
            bool ret = true;

            if (ClientSettings.CheckVersion)
            {
                Checker = new UpgradeChecker();
                Checker.UpgradeFound += new UpgradeChecker.delUpgradeFound(UpdateChecker_UpdateFound);
            }
            SetUpListControl();

            try
            {
                ResetDictionaries();
            }
            catch (OutOfMemoryException)
            {
                PockeTwit.Localization.LocalizedMessageBox.Show("There's not enough memory to run WeGo. You may want to close some applications and try again.");
                if (Manager != null)
                {
                    Manager.ShutDown();
                }
                this.Close();
            }
            catch (Exception ex)
            {
                PockeTwit.Localization.LocalizedMessageBox.Show("Corrupt settings - {0}\r\nPlease reconfigure.", ex.Message);
                ClearSettings();
                ResetDictionaries();
            }

            CurrentlySelectedAccount = ClientSettings.DefaultAccount;

            Notifyer = new NotificationHandler();
            Notifyer.MessagesNotificationClicked += new NotificationHandler.DelNotificationClicked(Notifyer_MessagesNotificationClicked);

            return ret;

        }

        private void ClearSettings()
        {
            if (System.IO.File.Exists(ClientSettings.AppPath + "\\app.config"))
            {
                System.IO.File.Delete(ClientSettings.AppPath + "\\app.config");
            }
            ClientSettings.AccountsList.Clear();
            using (AccountsForm f = new AccountsForm())
            {
                f.Owner = this;
                f.ShowDialog();
                if (ClientSettings.AccountsList.Count == 0)
                {
                    if (Manager != null)
                    {
                        Manager.ShutDown();
                    }
                    this.Close();
                }
                f.Dispose();
            }
        }

        void Notifyer_MessagesNotificationClicked()
        {
            this.Show();
        }

        private void ResetDictionaries()
        {
            FollowingDictionary.Clear();
            TwitterConnections.Clear();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Twitter.Account AccountInfo = new Twitter.Account
                {
                    ServerURL = a.ServerURL,
                    UserName = a.UserName,
                    UserID = a.UserID,
                    Password = a.Password,
                    OAuth_token = a.OAuth_token,
                    OAuth_token_secret = a.OAuth_token_secret,
                    Enabled = a.Enabled
                };
                Twitter TwitterConn = Servers.CreateConnection(AccountInfo);
                TwitterConnections.Add(TwitterConn);
                Following f = new Following(TwitterConn);
                FollowingDictionary.Add(TwitterConn, f);
            }
            SetConnectedMenus();
            Manager = new TimelineManagement();
            Manager.Progress += Manager_Progress;
            Manager.CompleteLoaded += Manager_CompleteLoaded;
            Manager.Startup(TwitterConnections);
            Manager.FriendsUpdated += Manager_FriendsUpdated;
            Manager.MessagesUpdated += Manager_MessagesUpdated;
            Manager.SearchesUpdated += Manager_SearchesUpdated;

            foreach (Following f in FollowingDictionary.Values)
            {
                f.LoadFromTwitter();
            }
        }

        void Manager_SearchesUpdated()
        {
            if (currentSpecialTimeLine != null && statList.CurrentList()== currentSpecialTimeLine.ListName)
            {
                AddStatusesToList(Manager.GetGroupedTimeLine(currentSpecialTimeLine, Yedda.Twitter.PagingMode.None), true);
                if (currentSpecialTimeLine.Timelinetype == SpecialTimeLinesRepository.TimeLineType.SavedSearch)
                    statList.AddItem(new MoreResultsItem(this, currentSpecialTimeLine));

            }
            LastSelectedItems.UpdateUnreadCounts();
            Notifyer.NewItems();
        }


        void Manager_CompleteLoaded()
        {
            if (InvokeRequired)
            {
                delNone d = new delNone(Manager_CompleteLoaded);
                this.Invoke(d);
            }
            else
            {
                Application.DoEvents();
                statList.SwitchTolist("Friends_TimeLine");

                AddStatusesToList(Manager.GetFriendsImmediately());
                statList.Startup = false;
                if (!StartBackground)
                {
                    statList.Visible = true;
                }
                //statList.SetSelectedIndexToZero();
                progressBar1.Visible = false;
                lblTitle.Visible = false;
                Application.DoEvents();
                statList.Repaint();
                
            }
        }

        void Manager_Progress(int percentage, string Status)
        {
            lblProgress.Visible = true;
            lblProgress.Text = Status;
            progressBar1.Visible = false;
            Application.DoEvents();
        }

        
        delegate void delText(string Text);
        private void setCaption(string text)
        {
            if (InvokeRequired)
            {
                delText d = new delText(setCaption);
                this.BeginInvoke(d, text);
            }
            else
            {
                this.Text = "WeGo" + text;
            }
        }

        void Manager_FriendsUpdated()
        {
            if (statList.CurrentList() == "Friends_TimeLine")
            {
                AddStatusesToList(Manager.GetFriendsImmediately(), true);
            }
            else
            {
                if(currentSpecialTimeLine!=null && statList.CurrentList() == currentSpecialTimeLine.ListName)
                {
                    AddStatusesToList(Manager.GetGroupedTimeLine(currentSpecialTimeLine, Yedda.Twitter.PagingMode.None), true);
                    if (currentSpecialTimeLine.Timelinetype == SpecialTimeLinesRepository.TimeLineType.SavedSearch)
                        statList.AddItem(new MoreResultsItem(this, currentSpecialTimeLine));

                }
            }
            LastSelectedItems.UpdateUnreadCounts();
            Notifyer.NewItems();
        }
        void Manager_MessagesUpdated()
        {
            if (statList.CurrentList() == "Messages_TimeLine")
            {
                AddStatusesToList(Manager.GetMessagesImmediately(), true);
            }
            Notifyer.NewItems();
            LastSelectedItems.UpdateUnreadCounts();
        }

        //private void MapList()
        //{
        //    Cursor.Current = Cursors.WaitCursor;
        //    Application.DoEvents();
        //    using (ProfileMap m = new ProfileMap())
        //    {
        //        List<Library.User> users = new List<Library.User>();
        //        for (int i = 0; i < statList.m_items.Count; i++)
        //        {
        //            Library.User thisUser = statList.m_items[i].Tweet.user;
        //            if (thisUser.needsFetching)
        //            {
        //                thisUser = Library.User.FromId(thisUser.screen_name, statList.m_items[i].Tweet.Account);
        //                thisUser.needsFetching = false;
        //            }
        //            users.Add(thisUser);
        //        }
        //        m.Users = users;
        //        m.ShowDialog();
        //        if (m.Range > 0)
        //        {

        //            SearchForm f = new SearchForm();
        //            f.providedDistnce = m.Range.ToString();
        //            string secondLoc = Geocode.GetAddress(m.CenterLocation.ToString());
        //            if (string.IsNullOrEmpty(secondLoc))
        //            {
        //                secondLoc = m.CenterLocation.ToString();
        //            }

        //            f.providedLocation = secondLoc;

        //            this.statList.Visible = false;
        //            IsLoaded = false;
        //            if (f.ShowDialog() == DialogResult.Cancel)
        //            {
        //                IsLoaded = true;
        //                this.statList.Visible = true;
        //                f.Close();
        //                return;
        //            }

        //            IsLoaded = true;
        //            this.statList.Visible = true;
        //            f.Hide();
        //            string SearchString = f.SearchText;
        //            f.Close();
        //            this.statList.Visible = true;

        //            ShowSearchResults(SearchString);
        //        }
        //        m.Close();
        //    }
        //}

        private void ToggleFullScreen()
        {
            ClientSettings.IsMaximized = !ClientSettings.IsMaximized;
            ClientSettings.SaveSettings();
            if (ClientSettings.IsMaximized)
            {
                this.WindowState = FormWindowState.Maximized;
                this.Menu = null;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                AddMainMenuItems();
            }
        }

        private void SetStatus()
        {
            SetStatus("", "");
        }

        private void SetStatus(string Text, string in_reply_to_status_id)
        {
            using (PostUpdate StatusForm = new PostUpdate(false))
            {
                StatusForm.Owner = this;
                if (!string.IsNullOrEmpty(Text))
                {
                    StatusForm.AccountToSet = CurrentlySelectedAccount;
                }
                else
                {
                    StatusForm.AccountToSet = ClientSettings.DefaultAccount;
                }
                this.statList.Visible = false;
                if (!string.IsNullOrEmpty(Text))
                {
                    StatusForm.StatusText = Text + " ";
                }
                IsLoaded = false;
                StatusForm.in_reply_to_status_id = in_reply_to_status_id;
                Manager.Pause();
                if (StatusForm.ShowDialog() == DialogResult.OK)
                {
                    this.statList.Visible = true;
                    IsLoaded = false;
                    //Manager.RefreshFriendsTimeLine();
                }
                else
                {
                    this.statList.Visible = true;
                    IsLoaded = false;
                }
                Manager.Start();
                this.Visible = true;
                IsLoaded = true;
                this.statList.Redraw();
                this.statList.Visible = true;
                StatusForm.Dispose();
            }
        }

        private void SetUpListControl()
        {
            statList.IsMaximized = ClientSettings.IsMaximized;
            statList.WordClicked += new StatusItem.ClickedWordDelegate(statusList_WordClicked);
            statList.SelectedItemChanged += new EventHandler(statusList_SelectedItemChanged);
            statList.HookKey();
        }

        private void ShowAbout()
        {
            using (AboutForm a = new AboutForm())
            {
                IsLoaded = false;
                statList.Visible = false;
                a.Owner = this;
                a.ShowDialog();

                this.Visible = true;

                statList.Visible = true;
                IsLoaded = true;
                string ReqedUser = a.AskedToSeeUser;
                a.Dispose();

                if (!string.IsNullOrEmpty(ReqedUser))
                {
                    statList.IgnoreMouse = true;
                    SwitchToUserTimeLine(a.AskedToSeeUser);
                }
            }
        }
        private void EmailThisItem()
        {
            if ((statList.SelectedItem == null) || (statList.SelectedItem as StatusItem == null)) { return; }
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            Microsoft.WindowsMobile.PocketOutlook.OutlookSession sess = new Microsoft.WindowsMobile.PocketOutlook.OutlookSession();
            Microsoft.WindowsMobile.PocketOutlook.EmailAccountCollection accounts = sess.EmailAccounts;

            if (accounts.Count == 0)
            {
                PockeTwit.Localization.LocalizedMessageBox.Show("You don't have any email accounts set up on this phone.");
                return;
            }
            else if (accounts.Count>1)
            {
                using (EmailStatusForm f = new EmailStatusForm(selectedItem.Tweet.text))
                {
                    f.Owner = this;
                    f.ShowDialog();
                    f.Dispose();
                }
                return;
            }
            
            Microsoft.WindowsMobile.PocketOutlook.EmailMessage m = new Microsoft.WindowsMobile.PocketOutlook.EmailMessage();
            //m.BodyText = selectedItem.Tweet.text;
            m.BodyText = "@" + selectedItem.Tweet.user.screen_name + ": " + selectedItem.Tweet.text;
            Microsoft.WindowsMobile.PocketOutlook.MessagingApplication.DisplayComposeForm(accounts[0], m);
        }
        private void ShowProfile()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }

            ChangeCursor(Cursors.WaitCursor);
            IProfileViewer view;
            if (DetectDevice.DeviceType == DeviceType.Professional)
            {
                view = new ProfileView(selectedItem.Tweet.user);
            }
            else
            {
                view = new ProfileViewSmartPhone(selectedItem.Tweet.user);
            }

            ChangeCursor(Cursors.Default);

            IsLoaded = false;
            statList.Visible = false;

            ((Form)view).Owner = this;
            ((Form)view).ShowDialog();

            this.Visible = true;
            statList.Visible = true;
            IsLoaded = true;

            //statList.OpenLeftMenu();

            //statList.RightMenu.SelectedItem = null;
            //statList.LeftMenu.SelectedItem = null;

            ((Form)view).Close();

            if (String.IsNullOrEmpty(view.selectedUser))
            {
                return;
            }

            statList.IgnoreMouse = true;

            if (view.selectedAction == ProfileAction.UserTimeline)
            {
                SwitchToUserTimeLine(view.selectedUser);
            }
            else if (view.selectedAction == ProfileAction.Favorites)
            {
                SwitchToUserFavorites(view.selectedUser);
            }
            else if (view.selectedAction == ProfileAction.Followers)
            {
                //TODO: 显示粉丝详细信息
                SwitchToUserFollowers(view.selectedUser);
            }
            else if (view.selectedAction == ProfileAction.Following)
            {
                //TODO: 显示关注详细信息
                SwitchToUserFriends(view.selectedUser);
            }
            else
            {
                statList.IgnoreMouse = false; //is this needed?
            }
            ((Form)view).Dispose();
        }

        private void ExitApplication()
        {
            Cursor.Current = Cursors.WaitCursor;
            if (Notifyer != null) { Notifyer.ShutDown(); }
            if (Manager != null)
            {
                Manager.ShutDown();
            }
            ThrottledArtGrabber.running = false;
            Cursor.Current = Cursors.Default;
            this.Close();
        }

        private void GoBackInHistory()
        {
            
            if (History.Count > 0)
            {
                HistoryItem prev = null;
                try
                {
                    HistoryItem current = History.Pop();
                    prev = History.Pop();
                }
                catch
                {
                    return;
                }
                switch (prev.Action)
                {
                    case Yedda.Twitter.ActionType.Conversation:
                        GetConversation(prev);
                        break;
                    case Yedda.Twitter.ActionType.Friends_Timeline:
                        ShowFriendsTimeLine();
                        statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);
                        break;
                    case Yedda.Twitter.ActionType.Replies:
                        statList.SetSelectedMenu(RefreshMessagesMenuItem);
                        ShowMessagesTimeLine();
                        break;
                    case Yedda.Twitter.ActionType.Search:
                        if (prev.ItemInfo == null)
                        {
                            statList.SetSelectedMenu(SearchMenuItem);
                            if (CurrentList == "Search_Timeline" && LastSearchTerm == prev.Argument)
                            {
                                ShowSearchResults(prev.Argument, false, Twitter.PagingMode.Back);
                            }
                            else
                            {
                                ShowSearchResults(prev.Argument, false, Twitter.PagingMode.Neutral);
                            }
                        }
                        else
                        {
                            if (CurrentList.StartsWith("SavedSearch_TimeLine_") && currentSpecialTimeLine != null && prev.Argument == currentSpecialTimeLine.name)
                            {
                                ShowSpecialTimeLine(prev.ItemInfo as ISpecialTimeLine, Yedda.Twitter.PagingMode.Back);
                            }
                            else
                            {
                                ShowSpecialTimeLine(prev.ItemInfo as ISpecialTimeLine, Yedda.Twitter.PagingMode.Neutral);
                            }
                        }
                        break;
                    case Yedda.Twitter.ActionType.User_Timeline:
                        statList.SetSelectedMenu(UserTimelineMenuItem);
                        SwitchToUserTimeLine(prev.Argument);
                        break;
                }
                if (prev.SelectedItemIndex >= 0)
                {
                    try
                    {
                        statList.SelectedItem = statList[prev.SelectedItemIndex];
                    }
                    catch (KeyNotFoundException) { }
                }
                else
                {
                    statList.SelectedItem = statList[0];
                }
                if (prev.itemsOffset >= 0)
                {
                    statList.YOffset = prev.itemsOffset;
                    statList.Redraw();
                    statList.RerenderPortal();
                    statList.Repaint();
                }
            }
        }
        private void ShowFavorites()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            
            SwitchToList("Favorites_TimeLine");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Favorites;
            History.Push(i);
            statList.SetSelectedMenu(ViewFavoritesMenuItem);
            AddStatusesToList(Manager.GetFavorites());       
            ChangeCursor(Cursors.Default);
        }

        private void ShowPublicTimeLine()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            
            SwitchToList("Public_TimeLine");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Public_Timeline;
            History.Push(i);
            statList.SetSelectedMenu(PublicMenuItem);
            AddStatusesToList(Manager.GetPublicTimeLine());
            ChangeCursor(Cursors.Default);
        }

        private void ShowSendDirectMessagesTimeLine()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);

            SwitchToList("SendDirectMessages_TimeLine");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Send_Direct_Messages;
            History.Push(i);
            statList.SetSelectedMenu(SendDirectMessagesMenuItem);
            AddStatusesToList(Manager.GetSendDirectMessagesTimeLine());
            ChangeCursor(Cursors.Default);
        }

        private void RefreshFriendsTimeLine()
        {
            Manager.RefreshFriendsTimeLine();
            statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);
        }

        private void ShowFriendsTimeLine()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            bool Redraw = statList.CurrentList() != "Friends_TimeLine";
            SwitchToList("Friends_TimeLine");
            History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Friends_Timeline;
            History.Push(i);
            statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);
            AddStatusesToList(Manager.GetFriendsImmediately());
            ChangeCursor(Cursors.Default);
        }

        private void RefreshMessagesTimeLine()
        {
            Manager.RefreshMessagesTimeLine();
            statList.SetSelectedMenu(RefreshMessagesMenuItem);
        }

        private void ShowMessagesTimeLine()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            SwitchToList("Messages_TimeLine");
            History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Replies;
            History.Push(i);
            statList.SetSelectedMenu(RefreshMessagesMenuItem);
            AddStatusesToList(Manager.GetMessagesImmediately());
            ChangeCursor(Cursors.Default);
        }

        private void ShowUserTimeLine()
        {
            currentSpecialTimeLine = null;
            UpdateHistoryPosition();
            ChangeCursor(Cursors.WaitCursor);
            StatusItem statItem = statList.SelectedItem as StatusItem;
            if (statItem == null) { return; }
            
            //if(statItem.Tweet.Account.Server == Twitter.TwitterServer.sohu)
            //    ShowUserID = statItem.Tweet.user.id;
            //else
            //    ShowUserID = statItem.Tweet.user.screen_name;
            ShowUserID = statItem.Tweet.user.id;

            CurrentlySelectedAccount = statItem.Tweet.Account;
            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);
            SwitchToList("@User_TimeLine");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.User_Timeline;
            i.Account = CurrentlySelectedAccount;
            i.Argument = ShowUserID;
            History.Push(i);
            //if getting authenticating user's timeline, you can merge in their retweets
            if (CurrentlySelectedAccount.UserName == ShowUserID)
            {
                List<status> tempList = new List<status>();
                tempList.AddRange(Manager.GetUserTimeLine(Conn, ShowUserID));
                tempList.AddRange(Manager.GetRetweetedByMe(Conn, ShowUserID));
                tempList.Sort();
                if (tempList.Count > 20)
                {
                    tempList.RemoveRange(20, tempList.Count - 20); 
                }
                AddStatusesToList(tempList.ToArray());
            }
            else
            {
                AddStatusesToList(Manager.GetUserTimeLine(Conn, ShowUserID));
            }
            ChangeCursor(Cursors.Default);

            return;
        }

        private void DeleteStatus()
        {
            StatusItem s = statList.SelectedItem as StatusItem;
            if (s != null)
            {
                if (s.Tweet.Delete())
                {
                    GlobalEventHandler.CallShowErrorMessage("Delete success.");
                    // refresh
                    AddStatusesToList(Manager.GetFriendsImmediately());
                }
                else
                {
                    GlobalEventHandler.CallShowErrorMessage("Could not delete.");
                }
            }
        }

        private void GetConversation()
        {
            GetConversation(null);
        }

        private void GetConversation(HistoryItem history)
        {
            currentSpecialTimeLine = null;
            UpdateHistoryPosition();
            HistoryItem i = new HistoryItem();
            Library.status lastStatus;
            Yedda.Twitter Conn;

            if (history == null)
            {
                StatusItem selectedItem = statList.SelectedItem as StatusItem;
                if (selectedItem == null) { return; }
                if (string.IsNullOrEmpty(selectedItem.Tweet.in_reply_to_status_id)) { return; }
                Conn = GetMatchingConnection(selectedItem.Tweet.Account);
                lastStatus = selectedItem.Tweet;

                i.Account = selectedItem.Tweet.Account;
                i.Action = Yedda.Twitter.ActionType.Conversation;
                i.Argument = lastStatus.id;
            }
            else
            {
                i = history;
                Conn = GetMatchingConnection(history.Account);
                try
                {
                    lastStatus = Library.status.DeserializeSingle(Conn.ShowSingleStatus(i.Argument), i.Account);
                }
                catch
                {
                    return;
                }
            }
            ChangeCursor(Cursors.WaitCursor);

            //List<Library.status> Conversation = GetConversationFROMTHEFUTURE(lastStatus);
            List<Library.status> Conversation = new List<PockeTwit.Library.status>();
            History.Push(i);

            while (!string.IsNullOrEmpty(lastStatus.in_reply_to_status_id))
            {
                Conversation.Add(lastStatus);
                try
                {
                    lastStatus = Library.status.DeserializeSingle(Conn.ShowSingleStatus(lastStatus.in_reply_to_status_id), Conn.AccountInfo);
                }
                catch
                {
                    lastStatus = null;
                    break;
                }
            }
            if (lastStatus != null)
            {
                Conversation.Add(lastStatus);
            }
            statList.SwitchTolist("Conversation");
            statList.ClearVisible();
            AddStatusesToList(Conversation.ToArray());
            ChangeCursor(Cursors.Default);
            this.SetLeftMenu();
        }

        private List<PockeTwit.Library.status> GetConversationFROMTHEFUTURE(PockeTwit.Library.status lastStatus)
        {
            Yedda.Twitter Conn = GetMatchingConnection(lastStatus.Account);
            Library.status[] SearchResults = Manager.SearchTwitter(Conn, "@" + lastStatus.user.screen_name, Yedda.Twitter.PagingMode.None);
            List<Library.status> Results = new List<PockeTwit.Library.status>();
            foreach (Library.status s in SearchResults)
            {
                if (s.in_reply_to_status_id == lastStatus.id)
                {
                    Results.Add(s);
                }
            }

            if (Results.Count == 1)
            {
                Results.AddRange(GetConversationFROMTHEFUTURE(Results[0]));
            }
            return Results;
        }


        private void Quote()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }

            //if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sina)
            if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sina ||
                selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sohu ||
                selectedItem.Tweet.Account.Server == Twitter.TwitterServer.netease ||
                selectedItem.Tweet.Account.Server == Twitter.TwitterServer.digu)
            {
                using (PostUpdate StatusForm = new PostUpdate(false, PostUpdate.PostType.comment))
                {
                    StatusForm.Owner = this;
                    if (string.IsNullOrEmpty(selectedItem.Tweet.RTID))
                    {
                        StatusForm.retweet_status_id = selectedItem.Tweet.id;
                    }
                    else
                    {
                        StatusForm.comment_status_text = selectedItem.Tweet.text;
                        StatusForm.retweet_status_id = selectedItem.Tweet.RTID;

                        // 同时评论给原文作者
                        StatusForm.comment_org_status_id = selectedItem.Tweet.RTID;
                        StatusForm.comment_org_username = selectedItem.Tweet.RTUserName;
                    }
                    // 同时评论给xxx
                    StatusForm.comment_status_id = selectedItem.Tweet.id;
                    StatusForm.comment_username = selectedItem.Tweet.user.screen_name;
                    StatusForm.AccountToSet = selectedItem.Tweet.Account;

                    this.statList.Visible = false;
                    IsLoaded = false;
                    Manager.Pause();
                    if (StatusForm.ShowDialog() == DialogResult.OK)
                    {
                        this.statList.Visible = true;
                        IsLoaded = false;
                        //Manager.RefreshFriendsTimeLine();
                    }
                    else
                    {
                        this.statList.Visible = true;
                        IsLoaded = false;
                    }
                    Manager.Start();
                    this.Visible = true;
                    IsLoaded = true;
                    this.statList.Redraw();
                    this.statList.Visible = true;
                    StatusForm.Dispose();
                }
            }
            else
            {
                //string quote = "RT @" + selectedItem.Tweet.user.screen_name + ": " + selectedItem.Tweet.text;
                string quote = "@" + selectedItem.Tweet.user.screen_name + ": " + selectedItem.Tweet.text;
                SetStatus(quote, "");
            }
        }

        private void Retweet()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }

            //if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sina)
            if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sina ||
                selectedItem.Tweet.Account.Server == Twitter.TwitterServer.sohu ||
                selectedItem.Tweet.Account.Server == Twitter.TwitterServer.digu)
            {
                using (PostUpdate StatusForm = new PostUpdate(false, PostUpdate.PostType.repost))
                {
                    StatusForm.Owner = this;
                    if (string.IsNullOrEmpty(selectedItem.Tweet.RTID))
                    {
                        StatusForm.StatusText = PockeTwit.Localization.XmlBasedResourceManager.GetString("WeGo Repost");
                        StatusForm.retweet_status_id = selectedItem.Tweet.id;
                    }
                    else
                    {
                        // 同时评论给原文作者
                        StatusForm.comment_org_status_id = selectedItem.Tweet.RTID;
                        StatusForm.comment_org_username = selectedItem.Tweet.RTUserName;

                        StatusForm.StatusText = " //@" + selectedItem.Tweet.user.screen_name + ":" + selectedItem.Tweet.text;
                        StatusForm.retweet_status_id = selectedItem.Tweet.RTID;
                    }
                    // 同时评论给xxx
                    StatusForm.comment_status_id = selectedItem.Tweet.id;
                    StatusForm.comment_username = selectedItem.Tweet.user.screen_name;
                    StatusForm.AccountToSet = selectedItem.Tweet.Account;

                    this.statList.Visible = false;
                    IsLoaded = false;
                    Manager.Pause();
                    if (StatusForm.ShowDialog() == DialogResult.OK)
                    {
                        this.statList.Visible = true;
                        IsLoaded = false;
                        //Manager.RefreshFriendsTimeLine();
                    }
                    else
                    {
                        this.statList.Visible = true;
                        IsLoaded = false;
                    }
                    Manager.Start();
                    this.Visible = true;
                    IsLoaded = true;
                    this.statList.Redraw();
                    this.statList.Visible = true;
                    StatusForm.Dispose();
                }
            }
            else if (selectedItem.Tweet.Account.Server == Twitter.TwitterServer.netease)
            {
                if (selectedItem.Tweet.Account.UserID == selectedItem.Tweet.user.id)
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Sorry but you can't retweet your own tweets.");
                    return;
                }

                ChangeCursor(Cursors.WaitCursor);

                Yedda.Twitter TwitterConn = Servers.CreateConnection(selectedItem.Tweet.Account);

                string retValue = TwitterConn.Retweet_Status(selectedItem.Tweet.id, Yedda.Twitter.OutputFormatType.XML);

                if (string.IsNullOrEmpty(retValue))
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Error retweeting status -- empty response.  You may want to try again later.");
                    ChangeCursor(Cursors.Default);
                    return;
                }
                try
                {
                    //Library.status.DeserializeSingle(retValue, selectedItem.Tweet.Account);
                    if (0 == retValue.IndexOf("retweeted_status"))
                    {
                        PockeTwit.Localization.LocalizedMessageBox.Show("Error retweeting status -- empty response.  You may want to try again later.");
                        ChangeCursor(Cursors.Default);
                        return;

                    }
                }
                catch
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Error retweeting status -- bad response.  You may have already retweeted this.");
                    ChangeCursor(Cursors.Default);
                    return;
                }
                GlobalEventHandler.CallShowErrorMessage("Retweet success.");
                ChangeCursor(Cursors.Default);
            }
            else
            {
                if (selectedItem.Tweet.Account.UserName == selectedItem.Tweet.user.screen_name)
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Sorry but you can't retweet your own tweets.");
                    return;
                }

                ChangeCursor(Cursors.WaitCursor);

                Yedda.Twitter TwitterConn = Servers.CreateConnection(selectedItem.Tweet.Account);

                string retValue = TwitterConn.Retweet_Status(selectedItem.Tweet.id, Yedda.Twitter.OutputFormatType.XML);

                if (string.IsNullOrEmpty(retValue))
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Error retweeting status -- empty response.  You may want to try again later.");
                    ChangeCursor(Cursors.Default);
                    return;
                }
                try
                {
                    Library.status.DeserializeSingle(retValue, selectedItem.Tweet.Account);
                }
                catch
                {
                    PockeTwit.Localization.LocalizedMessageBox.Show("Error retweeting status -- bad response.  You may have already retweeted this.");
                    ChangeCursor(Cursors.Default);
                    return;
                }
                ChangeCursor(Cursors.Default);
            }
        }

        Type _lastSelectedItemType = typeof(StatusItem);

        void statusList_SelectedItemChanged(object sender, EventArgs e)
        {
            Type selectedType = statList.SelectedItem.GetType();
            if (selectedType != _lastSelectedItemType)
            {
                // item type has changed, update menus
                CreateRightMenu();
                UpdateRightMenu();
                _lastSelectedItemType = selectedType;
            }

            StatusItem statItem = statList.SelectedItem as StatusItem;
            if (statItem == null) { return; }
            CurrentlySelectedAccount = statItem.Tweet.Account;
            SetConnectedMenus(GetMatchingConnection(CurrentlySelectedAccount), statItem);
            //UpdateRightMenu(); -- THIS IS DONE IN SETCONNECTEDMENUS
            UpdateHistoryPosition();
            int clickedNumber = statItem.Index + 1;
            SetLeftMenu();            
            LastSelectedItems.SetLastSelected(statList.CurrentList(), statItem.Tweet, currentSpecialTimeLine);
        }

        private void UpdateHistoryPosition()
        {
            if (History.Count > 0)
            {
                HistoryItem i = History.Peek();
                i.SelectedItemIndex = statList.SelectedIndex;
                i.itemsOffset = statList.YOffset;
            }
        }


        private bool TextIsUrl(string text)
        {
            String testString = string.Empty;
            if (text.StartsWith("http", StringComparison.CurrentCultureIgnoreCase))
            {
                return true;
                //testString = "http://" + text;
            }
            //Regex RgxUrl = new Regex("(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
            //return RgxUrl.IsMatch(testString);

            return false;
        }

        void statusList_WordClicked(string TextClicked)
        {
            //Todo: Popup with options to open or do nothing.
            if (TextIsUrl(TextClicked))
            {
                if (PockeTwit.MediaServices.PictureServiceFactory.Instance.FetchServiceAvailable(TextClicked)
                    && !ClientSettings.DisableAllPreview)
                {
                    Cursor.Current = Cursors.WaitCursor;
                    PockeTwit.MediaServices.IPictureService p = PockeTwit.MediaServices.PictureServiceFactory.Instance.LocateFetchService(TextClicked);
                    p.FetchPicture(TextClicked, CurrentlySelectedAccount);
                    p.DownloadFinish += new PockeTwit.MediaServices.DownloadFinishEventHandler(p_DownloadFinish);
                    p.ErrorOccured += new PockeTwit.MediaServices.ErrorOccuredEventHandler(p_ErrorOccured);
                    return;
                }

                //launch browser in a thread
                urlToLaunch = TextClicked;
                Thread t = new Thread(new ThreadStart(LaunchBrowserLink));
                t.Start();
            }
            else if (TextClicked.StartsWith("#"))
            {
                string searchString = TextClicked.Replace("#", "");
                //ShowSearchResults("q=" + System.Web.HttpUtility.UrlEncode(TextClicked));
                ShowSearchResults(System.Web.HttpUtility.UrlEncode(searchString));
            }
            else if (TextClicked.StartsWith("@"))
            {
                SwitchToUserTimeLine(TextClicked);
            }
        }

        void p_ErrorOccured(object sender, PockeTwit.MediaServices.PictureServiceEventArgs eventArgs)
        {
            if (InvokeRequired)
            {
                delPictureDone d = new delPictureDone(p_ErrorOccured);
                this.Invoke(d, sender, eventArgs);
            }
            else
            {
                PockeTwit.MediaServices.IPictureService p = (PockeTwit.MediaServices.IPictureService)sender;
                p.DownloadFinish -= new PockeTwit.MediaServices.DownloadFinishEventHandler(p_DownloadFinish);
                p.ErrorOccured -= new PockeTwit.MediaServices.ErrorOccuredEventHandler(p_ErrorOccured);
                Cursor.Current = Cursors.Default;
                PockeTwit.Localization.LocalizedMessageBox.Show("Unable to fetch picture. You may want to try again.");
            }
        }

        delegate void delPictureDone(object sender, PockeTwit.MediaServices.PictureServiceEventArgs eventArgs);
        void p_DownloadFinish(object sender, PockeTwit.MediaServices.PictureServiceEventArgs eventArgs)
        {
            if (InvokeRequired)
            {
                delPictureDone d = new delPictureDone(p_DownloadFinish);
                this.Invoke(d, sender, eventArgs);
            }
            else
            {


                PockeTwit.MediaServices.IPictureService p = (PockeTwit.MediaServices.IPictureService)sender;
                p.DownloadFinish -= new PockeTwit.MediaServices.DownloadFinishEventHandler(p_DownloadFinish);
                p.ErrorOccured -= new PockeTwit.MediaServices.ErrorOccuredEventHandler(p_ErrorOccured);

                Cursor.Current = Cursors.Default;

                using (ImagePreview ip = new ImagePreview(eventArgs.ReturnMessage, eventArgs.PictureFileName))
                {
                    ip.Owner = this;
                    ip.ShowDialog();
                    ip.Dispose();
                }
            }
        }

        private void SwitchToUserTimeLine(string TextClicked)
        {
            UpdateHistoryPosition();
            ShowUserID = TextClicked.Replace("@", "");
            StatusItem statItem = statList.SelectedItem as StatusItem;
            if (statItem == null) { return; }

            ChangeCursor(Cursors.WaitCursor);
            CurrentlySelectedAccount = statItem.Tweet.Account;
            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);

            if (statItem.Tweet.Account.Server == Twitter.TwitterServer.sohu)
            {
                string response = "";
                response = Conn.Show(ShowUserID, Twitter.OutputFormatType.XML);
                if (string.IsNullOrEmpty(response))
                {
                    GlobalEventHandler.CallShowErrorMessage("User not found.");
                }
                else
                {
                    var s = Library.User.DeserializeSingle(response);
                    if (!string.IsNullOrEmpty(s.id))
                    {
                        ShowUserID = s.id;
                    }
                    else
                    {
                        GlobalEventHandler.CallShowErrorMessage("User ID is empty.");
                    }
                }
            }
            else if (statItem.Tweet.Account.Server == Twitter.TwitterServer.digu)
            {
                ShowUserID = statItem.Tweet.user.id;
            }

            HistoryItem i = new HistoryItem();
            i.Argument = ShowUserID;
            i.Account = statItem.Tweet.Account;
            i.Action = Yedda.Twitter.ActionType.User_Timeline;
            History.Push(i);
            SwitchToList("@User_TimeLine");
            //if getting authenticating user's timeline, you can merge in their retweets
            if (CurrentlySelectedAccount.UserName == ShowUserID)
            {
                List<status> tempList = new List<status>();
                status[] UserTimeLine = Manager.GetUserTimeLine(Conn, ShowUserID);
                status[] UserRetweeted = Manager.GetRetweetedByMe(Conn, ShowUserID);
                // It's not a problem if there's an error
                if(UserTimeLine != null)
                    tempList.AddRange(UserTimeLine);
                if(UserRetweeted != null)
                    tempList.AddRange(UserRetweeted);
                tempList.Sort();
                if (tempList.Count > 20)
                {
                    tempList.RemoveRange(20, tempList.Count - 20);
                }
                AddStatusesToList(tempList.ToArray());
            }
            else
            {
                AddStatusesToList(Manager.GetUserTimeLine(Conn, ShowUserID));
            }
            ChangeCursor(Cursors.Default);
            return;
        }

        private void SwitchToDone()
        {

            //lblTitle.Visible = false;

            GlobalEventHandler.setPid();

            if (!StartBackground)
            {
                statList.Visible = true;
            }
            statList.BringToFront();
            SwitchToList("Friends_TimeLine");
            IsLoaded = true;
            lblTitle.Text = "WeGo";
            lblProgress.Visible = false;
            LastSelectedItems.UpdateUnreadCounts();
            this.Refresh();
            StartBackground = false;
        }
        private string CurrentList
        {
            get;
            set;
        }
        private void SwitchToList(string ListName)
        {
            if (statList.CurrentList() != ListName)
            {
                statList.SwitchTolist(ListName);
                CurrentList = ListName;
            }
            SetLeftMenu();
        }

        private void FollowUserClicked()
        {
            using (FollowUserForm f = new FollowUserForm())
            {
                f.Owner = this;
                if (f.ShowDialog() == DialogResult.Cancel)
                {
                    
                    f.Dispose();
                    return;
                }
                ChangeCursor(Cursors.WaitCursor);
                try
                {
                    Yedda.Twitter conn = GetMatchingConnection(f.Account);
                    string response = "";
                    if (f.Account.Server == Twitter.TwitterServer.sohu ||
                        f.Account.Server == Twitter.TwitterServer.digu)
                    {
                        response = conn.Show(f.UserName, Twitter.OutputFormatType.XML);
                        if (string.IsNullOrEmpty(response))
                        {
                            GlobalEventHandler.CallShowErrorMessage("User not found.");
                        }
                        else
                        {
                            var s = Library.User.DeserializeSingle(response);
                            if (!string.IsNullOrEmpty(s.id))
                            {
                                response = conn.FollowUser(s.id);

                                if (string.IsNullOrEmpty(response))
                                {
                                    GlobalEventHandler.CallShowErrorMessage("User not found.");
                                }
                                else
                                {
                                    FollowingDictionary[conn].AddUser(Library.User.FromId(f.UserName, f.Account));
                                    UpdateRightMenu();
                                    GlobalEventHandler.CallShowErrorMessage("Following succeed.");
                                }
                            }
                            else
                            {
                                GlobalEventHandler.CallShowErrorMessage("User ID is empty.");
                            }
                        }
                    }
                    else
                    {
                        response = conn.FollowUser(f.UserName);

                        if (string.IsNullOrEmpty(response))
                        {
                            GlobalEventHandler.CallShowErrorMessage("User not found.");
                        }
                        else
                        {
                            FollowingDictionary[conn].AddUser(Library.User.FromId(f.UserName, f.Account));
                            UpdateRightMenu();
                            GlobalEventHandler.CallShowErrorMessage("Following succeed.");
                        }
                    }
                }
                finally
                {
                    ChangeCursor(Cursors.Default);
                }
                f.Close();
            }
        }

        
        private void TwitterSearch()
        {

            using (SearchForm f = new SearchForm())
            {
                this.statList.Visible = false;
                IsLoaded = false;
                SavedSearchTimeLine SavedSearch;
                // setting this is the right thing to do, but kills the 
                // combobox - the drop-down list gets hidden behind the form
                // WEIRD.
                //f.Owner = this;
                if (f.ShowDialog() == DialogResult.Cancel)
                {
                    IsLoaded = true;
                    this.statList.Visible = true;
                    f.Dispose();
                    return;
                }

                IsLoaded = true;
                this.statList.Visible = true;
                f.Hide();
                string SearchString = f.SearchText;
                SavedSearch = f.SavedSearch;
                if(SavedSearch!=null)
                {
                    AddGroupSelectMenuItem(SavedSearch);
                }
                f.Dispose();

                statList.Visible = true;

                ShowSearchResults(SearchString, (SavedSearch!=null && SavedSearch.autoUpdate));
            }
        }

        private void ShowSearchResults(string SearchString)
        {
            ShowSearchResults(SearchString,false);
        }

        private void ShowSearchResults(string SearchString, bool saveThem)
        {
            ShowSearchResults(SearchString, saveThem, Yedda.Twitter.PagingMode.None);
        }

        internal string LastSearchTerm
        {
            get;
            set;
        }

        internal void ShowSearchResults(string SearchString, bool saveThem, Yedda.Twitter.PagingMode pagingMode)
        {
            LastSearchTerm = SearchString;
            UpdateHistoryPosition();
            ChangeCursor(Cursors.WaitCursor);
            statList.SetSelectedMenu(SearchMenuItem);
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Search;
            i.Argument = SearchString;
            History.Push(i);

            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);
            SwitchToList("Search_TimeLine");
            statList.ClearVisible();
            Library.status[] stats = Manager.SearchTwitter(Conn, SearchString, pagingMode);

            if (stats != null)
            {
                List<Library.status> searchResults = new List<status>(stats);
                if (saveThem)
                {
                    LocalStorage.DataBaseUtility.SaveItems(searchResults);
                }
                AddStatusesToList(searchResults.ToArray());

                // TODO 搜索话题结果不能显示"More"Item项，需要调试
                //statList.AddItem(new MoreResultsItem(this, SearchString, saveThem));
            }
            ChangeCursor(Cursors.Default);
        }

        void UpdateChecker_UpdateFound(UpgradeChecker.UpgradeInfo Info)
        {
            if (InvokeRequired)
            {
                delNothing d = new delNothing(UpdateChecker_UpdateFound);
                Invoke(d, Info);
            }
            else
            {
                using (UpgradeForm uf = new UpgradeForm())
                {
                    uf.Owner = this;
                    uf.NewVersion = Info;
                    uf.ShowDialog();
                    uf.Dispose();
                }
            }
        }

        private void SetWindowState(FormWindowState State)
        {
            if (InvokeRequired)
            {
                delSetWindowState d = new delSetWindowState(SetWindowState);
                this.Invoke(d, new object[] { State });
            }
            else
            {
                this.WindowState = State;
            }
        }

        private bool isChangingingWindowState = false;

        protected override void OnActivated(EventArgs e)
        {
            if (DetectDevice.DeviceType == DeviceType.Professional)
            {
                inputPanel1.Enabled = false;
                if (Notifyer != null)
                {
                    Notifyer.DismissBubbler();
                }
            }
            if (isChangingingWindowState) { return; }
            isChangingingWindowState = true;
            
            
            GlobalEventHandler.setPid();

           
            // JohnB2007: changed this in order to avoid unused warning for IsLoaded.
            // Will result in the same MSIL due to compiler optimization anyway and allows
            // to reuse the infrastructure if once needed.
            if (!IsLoaded)
            {
            //    isChangingingWindowState = false;
            //    return;
            }
            


            if (ClientSettings.IsMaximized)
            {
                SetWindowState(FormWindowState.Maximized);
                this.Menu = null;
            }
            else
            {
                SetWindowState(FormWindowState.Normal);
                AddMainMenuItems();
            }
            //How do we tell if it was launched in background and is not being activated by user?
            // This is occuring after the initial run/setup is complete.
            statList.Visible = true;
            if (DetectDevice.DeviceType == DeviceType.Standard)
            {
                statList.Focus();
            }
            isChangingingWindowState = false;
           
            SendToForground();

            this.Invalidate();
        }

        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

        const int SW_MINIMIZED = 6;

        void Minimize()
        {
            isChangingingWindowState = true;
            // The Taskbar must be enabled to be able to do a Smart Minimize
            statList.Visible = false;
            //this.FormBorderStyle = FormBorderStyle.FixedDialog;
            //statList.Visible = false;
            //this.WindowState = FormWindowState.Normal;
            //this.ControlBox = true;
            //this.MinimizeBox = true;
            //this.MaximizeBox = true;

            // Since there is no WindowState.Minimize, we have to P/Invoke ShowWindow
            /*
            statList.Clear();
             */
            ShowWindow(this.Handle, SW_MINIMIZED);
            isChangingingWindowState = false;
            GC.Collect();
            GC.WaitForPendingFinalizers();

        }

        [System.Runtime.InteropServices.DllImport("coredll.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        void SendToForground()
        {
            SetForegroundWindow(this.Handle);
        }

        protected override void OnClosed(EventArgs e)
        {
            Program.IgnoreDisposed = true;
            if (Manager != null)
            {
                Manager.ShutDown();
            }
            if (Notifyer != null)
            {
                Notifyer.ShutDown();
            }
            ThrottledArtGrabber.running = false;
            string tempDir = ClientSettings.AppPath + "\\temp";
            try
            {
                if (Directory.Exists(tempDir))
                {
                    Directory.Delete(tempDir, true);
                }
            }
            catch (Exception ex)
            {
                base.OnClosed(e);
            }
            base.OnClosed(e);
        }

        #endregion�Methods�

        private void TweetList_LostFocus(object sender, EventArgs e)
        {
            if (!statList.Focused)
            {
                statList.Visible = false;
            }
        }

        private void globalMenu_Click(object sender, EventArgs e)
        {
            if (statList.RightMenu.IsExpanded)
            {
                statList.RightMenu.CollapseExpandedMenu();
            }
            else
            {
                statList.OpenLeftMenu();
            }
            //statList.OpenLeftMenu();
        }

        private void specificMenu_Click(object sender, EventArgs e)
        {
            if (statList.LeftMenu.IsExpanded)
            {
                statList.LeftMenu.CollapseExpandedMenu();
            }
            else
            {
                statList.OpenRightMenu();
            }
            //statList.OpenRightMenu();
        }

        //handle requests from outside the UI -- either CLI args or custom messages
        public void ProcessArgs(string[] args)
        {
            BringToFront();
            // don't try and do anything if we have no accounts, or if the args are blank
            if (ClientSettings.AccountsList.Count > 0 && args.Length > 0)
            {
                // QuickPost called
                string Arg = args[0];

                if (Arg == "/QuickPost")
                {
                    SetStatus();
                }

                // the today plugin passes /Group=XXXXX as argument
                if (Arg.StartsWith("/Group="))
                {
                    string GroupName = Arg.Substring(Arg.IndexOf('=') + 1);
                    ISpecialTimeLine t = SpecialTimeLinesRepository.GetFromReadableName(GroupName);
                    if (t != null)
                    {
                        ShowSpecialTimeLine(t, Yedda.Twitter.PagingMode.None);
                        
                    }
                    if (GroupName == "Friends TimeLine")
                    {
                        ShowFriendsTimeLine();
                    }
                    if (GroupName == "Messages TimeLine")
                    {
                        ShowMessagesTimeLine();
                    }
                    
                    
                }
            }


        }


        private void ShowTrends()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);

            SwitchToList("TrendingTopics");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Favorites;  //TODO
            History.Push(i);
            statList.SetSelectedMenu(ViewFavoritesMenuItem);
           
            //test = LetsBeTrends.GetTrend("GoodNight");

            statList.Clear();

            ArrayList al = LetsBeTrends.GetCurrentTrends();

            foreach (Hashtable a in al)
            {
                TrendingTopic tt = new TrendingTopic();
                tt.Name = (String)a["name"];
                tt.LastTrended = (String)a["last_trended_at"];
                tt.FirstTrended = (String)a["first_trended_at"];
                tt.Query = (String)a["query"];
                try
                {
                    if (a.Contains("description"))
                    {
                        Hashtable h = (Hashtable)a["description"];
                        tt.Description = (String)h["text"];
                    }
                    else
                    {
                        tt.Description = "No description available.";
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error: " + ex.Message);
                    tt.Description = "No description available.";
                }
                statList.AddItem(new TrendingTopicItem(this, null, tt));
            }
            statList.SelectedItem = statList[0];
            statList.YOffset = 0;
            statList.Redraw();
            statList.RerenderPortal();
            statList.Repaint();

            ChangeCursor(Cursors.Default);
        }


        private void SwitchToUserFavorites(String userID)
        {
            //currentSpecialTimeLine = null;
            UpdateHistoryPosition();
            userID = userID.Replace("@", "");
            StatusItem statItem = (StatusItem)statList.SelectedItem;
            if (statItem == null) { return; }
            ChangeCursor(Cursors.WaitCursor);
            HistoryItem i = new HistoryItem();
            i.Argument = userID;
            i.Account = statItem.Tweet.Account;
            i.Action = Yedda.Twitter.ActionType.Favorites; //i.Action = Yedda.Twitter.ActionType.User_Timeline;
            History.Push(i);
            CurrentlySelectedAccount = statItem.Tweet.Account;
            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);
            SwitchToList("Favorites_TimeLine"); //SwitchToList("@User_TimeLine"); 
            AddStatusesToList(Manager.GetUserFavorites(userID)); //AddStatusesToList(Manager.GetUserTimeLine(Conn, ShowUserID));
            ChangeCursor(Cursors.Default);
            return;
        }

        private void LaunchBrowserLink()
        {
            System.Diagnostics.ProcessStartInfo pi = new System.Diagnostics.ProcessStartInfo();
            if (ClientSettings.UseSkweezer)
            {
                pi.FileName = Skweezer.GetSkweezerURL(urlToLaunch);
            }
            else
            {
                pi.FileName = urlToLaunch;
            }
            try
            {
                pi.UseShellExecute = true;
                System.Diagnostics.Process p = System.Diagnostics.Process.Start(pi);
            }
            catch
            {
                PockeTwit.Localization.LocalizedMessageBox.Show("There is no default web browser defined for the OS.");
            }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
        }

        private void ShowMyTimeLine()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            //bool Redraw = statList.CurrentList() != "Friends_TimeLine";
            SwitchToList("My_TimeLine");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.My_Timeline;
            History.Push(i);
            //statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);

            List<status> tempList = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Yedda.Twitter Conn = Servers.CreateConnection(a);
                tempList.AddRange(Manager.GetUserTimeLine(Conn, a.UserID));
                //AddStatusesToList(Manager.GetUserTimeLine(Conn, a.UserID));
            }
            tempList.Sort();
            AddStatusesToList(tempList.ToArray());
            //AddStatusesToList(Manager.GetFriendsImmediately());
            ChangeCursor(Cursors.Default);
        }

        private void ShowMyComments()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            SwitchToList("Comments_Timeline");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Comments_Timeline;
            History.Push(i);

            List<comment> tempList = new List<comment>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Yedda.Twitter Conn = Servers.CreateConnection(a);
                Library.comment[] comments = null;
                if (a.Server == Twitter.TwitterServer.netease)
                {
                    GlobalEventHandler.CallShowErrorMessage("Developing...");
                    return;
                }
                else
                    comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments_Timeline, null);
                if (comments.Length > 0)
                    tempList.AddRange(comments);
            }
            tempList.Sort();
            comment[] comments_to_me = tempList.ToArray();
            foreach (comment c in comments_to_me)
            {
                status s = new status
                {
                    id = c.id,
                    text = c.text,
                    TypeofMessage = c.TypeofMessage,
                    createdAt = c.createdAt,
                    user = c.user,
                };
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            ChangeCursor(Cursors.Default);
        }

        private void ShowMyReceivedComments()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            //bool Redraw = statList.CurrentList() != "Friends_TimeLine";
            SwitchToList("Comments_To_Me");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Comments_To_Me;
            History.Push(i);
            //statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);

            List<comment> tempList = new List<comment>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Library.comment[] comments = null;
                Yedda.Twitter Conn = Servers.CreateConnection(a);

                if (a.Server == Twitter.TwitterServer.netease)
                {
                    GlobalEventHandler.CallShowErrorMessage("Developing...");
                    ChangeCursor(Cursors.Default);
                    return;
                }

                if (a.Server == Twitter.TwitterServer.sohu)
                    comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments_Timeline, null);
                else if (a.Server == Twitter.TwitterServer.digu)
                    comments = new comment[0];
                else
                    comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments_To_Me, null);

                if(comments.Length > 0)
                    tempList.AddRange(comments);
                //Library.comment[] comment_to_me = Manager.GetComment(Conn);
            }
            tempList.Sort();
            comment[] comments_to_me = tempList.ToArray();
            foreach (comment c in comments_to_me)
            {
                status s = new status
                {
                    id = c.id,
                    text = c.text,
                    TypeofMessage = c.TypeofMessage,
                    createdAt = c.createdAt,
                    user = c.user,
                };
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            //AddStatusesToList(Manager.GetFriendsImmediately());
            ChangeCursor(Cursors.Default);
        }

        private void ShowMySendComments()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            SwitchToList("Comments_By_Me");
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Comments_By_Me;
            History.Push(i);
            //statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);

            List<comment> tempList = new List<comment>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Library.comment[] comments = null;
                Yedda.Twitter Conn = Servers.CreateConnection(a);

                if (a.Server == Twitter.TwitterServer.netease)
                {
                    GlobalEventHandler.CallShowErrorMessage("Developing...");
                    ChangeCursor(Cursors.Default);
                    return;
                }

                if (a.Server == Twitter.TwitterServer.sohu)
                    comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments_Timeline, null);
                else if (a.Server == Twitter.TwitterServer.digu)
                    comments = new comment[0];
                else
                    comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments_By_Me, null);

                if (comments.Length > 0)
                    tempList.AddRange(comments);
                //Library.comment[] comment_to_me = Manager.GetComment(Conn);
            }
            tempList.Sort();
            comment[] comments_to_me = tempList.ToArray();
            foreach (comment c in comments_to_me)
            {
                status s = new status
                {
                    id = c.id,
                    text = c.text,
                    TypeofMessage = c.TypeofMessage,
                    createdAt = c.createdAt,
                    user = c.user,
                };
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            //AddStatusesToList(Manager.GetFriendsImmediately());
            ChangeCursor(Cursors.Default);
        }

        private void ShowMyFriends()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            SwitchToList("Friends");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Friends;
            History.Push(i);

            List<follower> tempList = new List<follower>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Yedda.Twitter Conn = Servers.CreateConnection(a);
                Library.follower[] comments = null;
                if(a.Server == Twitter.TwitterServer.netease)
                    comments = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Friends, a.UserID);
                else
                    comments = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Friends, null);
                if (comments.Length > 0)
                    tempList.AddRange(comments);
            }
            //tempList.Sort();
            follower[] comments_to_me = tempList.ToArray();
            foreach (follower c in comments_to_me)
            {
                status s = null;
                if (c.status == null)
                    s = new status();
                else
                    s = c.status;

                User user = new User
                {
                    location = c.location,
                    description = c.description,
                    profile_image_url = c.profile_image_url,
                    id = c.id,
                    name = c.name,
                    screen_name = c.screen_name,
                    followers_count = c.followers_count,
                    friends_count = c.friends_count,
                    statuses_count = c.statuses_count,
                    favourites_count = c.favourites_count,
                    created_at = c.created_at,
                    url = c.url,
                    verified = c.verified,
                };
                s.user = user;
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            ChangeCursor(Cursors.Default);
        }

        private void ShowMyFollowers()
        {
            currentSpecialTimeLine = null;
            ChangeCursor(Cursors.WaitCursor);
            SwitchToList("Followers");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Followers;
            History.Push(i);

            List<follower> tempList = new List<follower>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Yedda.Twitter Conn = Servers.CreateConnection(a);
                Library.follower[] comments = null;
                if (a.Server == Twitter.TwitterServer.netease)
                    comments = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Followers, a.UserID);
                else
                    comments = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Followers, null);
                //Library.follower[] comments = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Followers, null);
                if (comments.Length > 0)
                    tempList.AddRange(comments);
            }
            //tempList.Sort();
            follower[] comments_to_me = tempList.ToArray();
            foreach (follower c in comments_to_me)
            {
                status s = null;
                if (c.status == null)
                    s = new status();
                else
                    s = c.status;

                User user = new User
                {
                    location = c.location,
                    description = c.description,
                    profile_image_url = c.profile_image_url,
                    id = c.id,
                    name = c.name,
                    screen_name = c.screen_name,
                    followers_count = c.followers_count,
                    friends_count = c.friends_count,
                    statuses_count = c.statuses_count,
                    favourites_count = c.favourites_count,
                    created_at = c.created_at,
                    url = c.url,
                    verified = c.verified,
                };
                s.user = user;
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            ChangeCursor(Cursors.Default);
        }

        private void SwitchToUserFriends(string TextClicked)
        {
            UpdateHistoryPosition();
            ShowUserID = TextClicked.Replace("@", "");
            StatusItem statItem = statList.SelectedItem as StatusItem;
            if (statItem == null) { return; }
            ChangeCursor(Cursors.WaitCursor);

            CurrentlySelectedAccount = statItem.Tweet.Account;
            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);

            ShowUserID = statItem.Tweet.user.id;
            //if (statItem.Tweet.Account.Server == Twitter.TwitterServer.sohu ||
            //    statItem.Tweet.Account.Server == Twitter.TwitterServer.digu)
            //{
            //    string response = "";
            //    response = Conn.Show(ShowUserID, Twitter.OutputFormatType.XML);
            //    if (string.IsNullOrEmpty(response))
            //    {
            //        GlobalEventHandler.CallShowErrorMessage("User not found.");
            //    }
            //    else
            //    {
            //        var s = Library.User.DeserializeSingle(response);
            //        if (!string.IsNullOrEmpty(s.id))
            //        {
            //            ShowUserID = s.id;
            //        }
            //        else
            //        {
            //            GlobalEventHandler.CallShowErrorMessage("User ID is empty.");
            //        }
            //    }
            //}

            HistoryItem i = new HistoryItem();
            i.Argument = ShowUserID;
            i.Account = statItem.Tweet.Account;
            i.Action = Yedda.Twitter.ActionType.Friends;
            History.Push(i);
            SwitchToList("@Friends");

            List<status> mergedStatus = new List<status>();
            follower[] comments_to_me = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Friends, ShowUserID);
            foreach (follower c in comments_to_me)
            {
                status s = null;
                if (c.status == null)
                    s = new status();
                else
                    s = c.status;

                User user = new User
                {
                    location = c.location,
                    description = c.description,
                    profile_image_url = c.profile_image_url,
                    id = c.id,
                    name = c.name,
                    screen_name = c.screen_name,
                    followers_count = c.followers_count,
                    friends_count = c.friends_count,
                    statuses_count = c.statuses_count,
                    favourites_count = c.favourites_count,
                    created_at = c.created_at,
                    url = c.url,
                    verified = c.verified,
                };
                s.user = user;
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            ChangeCursor(Cursors.Default);
            return;
        }

        private void SwitchToUserFollowers(string TextClicked)
        {
            UpdateHistoryPosition();
            ShowUserID = TextClicked.Replace("@", "");
            StatusItem statItem = statList.SelectedItem as StatusItem;
            if (statItem == null) { return; }
            ChangeCursor(Cursors.WaitCursor);
            CurrentlySelectedAccount = statItem.Tweet.Account;
            Yedda.Twitter Conn = GetMatchingConnection(CurrentlySelectedAccount);

            ShowUserID = statItem.Tweet.user.id;
            //if (statItem.Tweet.Account.Server == Twitter.TwitterServer.sohu ||
            //    statItem.Tweet.Account.Server == Twitter.TwitterServer.digu)
            //{
            //    string response = "";
            //    response = Conn.Show(ShowUserID, Twitter.OutputFormatType.XML);
            //    if (string.IsNullOrEmpty(response))
            //    {
            //        GlobalEventHandler.CallShowErrorMessage("User not found.");
            //    }
            //    else
            //    {
            //        var s = Library.User.DeserializeSingle(response);
            //        if (!string.IsNullOrEmpty(s.id))
            //        {
            //            ShowUserID = s.id;
            //        }
            //        else
            //        {
            //            GlobalEventHandler.CallShowErrorMessage("User ID is empty.");
            //        }
            //    }
            //}

            HistoryItem i = new HistoryItem();
            i.Argument = ShowUserID;
            i.Account = statItem.Tweet.Account;
            i.Action = Yedda.Twitter.ActionType.Followers;
            History.Push(i);
            SwitchToList("@Followers");

            List<status> mergedStatus = new List<status>();
            follower[] comments_to_me = Manager.GetFriends(Conn, Yedda.Twitter.ActionType.Followers, ShowUserID);
            foreach (follower c in comments_to_me)
            {
                status s = null;
                if (c.status == null)
                    s = new status();
                else
                    s = c.status;

                User user = new User
                {
                    location = c.location,
                    description = c.description,
                    profile_image_url = c.profile_image_url,
                    id = c.id,
                    name = c.name,
                    screen_name = c.screen_name,
                    followers_count = c.followers_count,
                    friends_count = c.friends_count,
                    statuses_count = c.statuses_count,
                    favourites_count = c.favourites_count,
                    created_at = c.created_at,
                    url = c.url,
                    verified = c.verified,
                };
                s.user = user;
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            ChangeCursor(Cursors.Default);
            return;
        }

        private void ViewComments()
        {
            StatusItem selectedItem = statList.SelectedItem as StatusItem;
            if (selectedItem == null) { return; }

            ChangeCursor(Cursors.WaitCursor);
            List<comment> tempList = new List<comment>();
            List<status> mergedStatus = new List<status>();
            foreach (Yedda.Twitter.Account a in ClientSettings.AccountsList)
            {
                Yedda.Twitter Conn = Servers.CreateConnection(a);
                Library.comment[] comments = Manager.GetComment(Conn, Yedda.Twitter.ActionType.Comments, selectedItem.Tweet.id);
                if (comments.Length > 0)
                    tempList.AddRange(comments);
                else
                {
                    GlobalEventHandler.CallShowErrorMessage("This status has no comments.");
                    ChangeCursor(Cursors.Default);
                    return;
                }
                //Library.comment[] comment_to_me = Manager.GetComment(Conn);
            }

            //currentSpecialTimeLine = null;
            //bool Redraw = statList.CurrentList() != "Friends_TimeLine";
            SwitchToList("Comments");
            //History.Clear();
            HistoryItem i = new HistoryItem();
            i.Action = Yedda.Twitter.ActionType.Comments;
            History.Push(i);
            //statList.SetSelectedMenu(RefreshFriendsTimeLineMenuItem);

            tempList.Sort();
            comment[] comments_to_me = tempList.ToArray();
            foreach (comment c in comments_to_me)
            {
                status s = new status
                {
                    id = c.id,
                    text = c.text,
                    TypeofMessage = c.TypeofMessage,
                    createdAt = c.createdAt,
                    user = c.user,
                };
                mergedStatus.Add(s);
            }
            AddStatusesToList(mergedStatus.ToArray());
            //AddStatusesToList(Manager.GetFriendsImmediately());
            ChangeCursor(Cursors.Default);
        }
    }
}
